public
Description: Prototype goodness
Clone URL: git://github.com/samleb/prototype-fruits.git
Search Repo:
slight changes
samleb (author)
Sun May 11 04:19:57 -0700 2008
commit  2ce4c44f304af1aa292453040a72bc2abce6a635
tree    901e05818020875981c40b5bb081f11eb102d35a
parent  c218548a2f551a1a581710bb58c8aa286d735cab
...
29
30
31
32
 
 
 
 
33
34
35
36
...
58
59
60
61
62
 
 
63
 
64
65
66
...
78
79
80
81
 
82
83
84
...
29
30
31
 
32
33
34
35
36
37
38
39
...
61
62
63
 
 
64
65
66
67
68
69
70
...
82
83
84
 
85
86
87
88
0
@@ -29,7 +29,10 @@
0
 
0
 Object.Base = Object.extend(function Base() { }, Concept);
0
 Object.Base.subclasses = [ ];
0
-Object.Base.addMethod('initialize', Prototype.emptyFunction);
0
+Object.Base.addMethods({
0
+ initialize: Prototype.emptyFunction,
0
+ extend: Object.extend.methodize()
0
+});
0
 
0
 var Class = {
0
   create: function(superclass) {
0
0
@@ -58,9 +61,10 @@
0
 
0
 var Mixin = Class.create(Concept, (function() {
0
   return {
0
- initialize: function(source) {
0
- this.prototype = source || { };
0
+ initialize: function() {
0
+ this.prototype = { };
0
       this.implementors = [ ];
0
+ $A(arguments).each(this.addMethods, this);
0
     },
0
 
0
     addMethod: Concept.addMethod.wrap(function(addMethod, name, block) {
0
@@ -78,7 +82,7 @@
0
   };
0
   
0
   function addMethodResolver(mixin, implementor, name) {
0
- if (!implementor.prototype[name]){
0
+ if (!implementor.prototype[name]) {
0
       implementor.addMethod(name, function() {
0
         return mixin.prototype[name].apply(this, arguments);
0
       });
...
18
19
20
21
 
22
23
24
25
26
...
29
30
31
32
 
 
33
34
 
35
36
37
38
39
40
41
42
43
...
51
52
53
54
 
55
56
57
 
 
58
59
60
...
65
66
67
68
69
 
 
70
71
...
18
19
20
 
21
22
23
24
25
26
...
29
30
31
 
32
33
34
 
35
36
37
38
 
 
39
40
41
42
...
50
51
52
 
53
54
 
 
55
56
57
58
59
...
64
65
66
 
 
67
68
69
70
0
@@ -18,7 +18,7 @@
0
   else return 0;
0
 });
0
 
0
-var Person = Class.create(Comparable, {
0
+var Player = Class.create(Comparable, {
0
   initialize: function(name, skill) {
0
     this.name = name;
0
     this.skill = skill;
0
0
0
@@ -29,14 +29,13 @@
0
   }
0
 });
0
 
0
-var Sortable = new Mixin({
0
+// An Enumerable whose elements are Comparable !
0
+var Sortable = new Mixin(Enumerable, {
0
   sort: function() {
0
- return this.sortBy(Prototype.K);
0
+ return this.toArray().sort(function(a, b) { return a.compareTo(b) });
0
   }
0
 });
0
 
0
-Sortable.addMethods(Enumerable);
0
-
0
 var Set = Class.create(Sortable, {
0
   initialize: function(enumerable) {
0
     this.elements = $A(enumerable);
0
0
@@ -51,10 +50,10 @@
0
   testMixin: function() {
0
     this.assertInstanceOf(Mixin, Comparable);
0
     this.assertRespondsTo('addMethod', Comparable);
0
- this.assertEnumEqual([Person], Comparable.implementors);
0
+ this.assertEnumEqual([Player], Comparable.implementors);
0
 
0
- var bobby = new Person('bobby', 20);
0
- var billy = new Person('billy', 30);
0
+ var bobby = new Player('bobby', 20);
0
+ var billy = new Player('billy', 30);
0
 
0
     this.assert(bobby.lowerThan(billy));
0
 
0
@@ -65,8 +64,8 @@
0
     this.assertRespondsTo('lowerThanOrEqualTo', bobby);
0
     this.assert(bobby.lowerThanOrEqualTo(billy));
0
 
0
- var set = new Set([3, 2, 1]);
0
- this.assertEnumEqual([1,2,3], set.sort());
0
+ var set = new Set([billy, bobby]);
0
+ this.assertEnumEqual([bobby, billy], set.sort());
0
   }
0
 });

Comments

    No one has commented yet.