-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathclass.js
179 lines (165 loc) · 6.09 KB
/
class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Based on Alex Arnell's inheritance implementation. */
/** section: Language
* class Class
*
* Manages Prototype's class-based OOP system.
*
* Refer to Prototype's web site for a [tutorial on classes and
* inheritance](http://prototypejs.org/learn/class-inheritance).
**/
var Class = (function() {
/**
* Class.create([superclass][, methods...]) -> Class
* - superclass (Class): The optional superclass to inherit methods from.
* - methods (Object): An object whose properties will be "mixed-in" to the
* new class. Any number of mixins can be added; later mixins take
* precedence.
*
* [[Class.create]] creates a class and returns a constructor function for
* instances of the class. Calling the constructor function (typically as
* part of a `new` statement) will invoke the class's `initialize` method.
*
* [[Class.create]] accepts two kinds of arguments. If the first argument is
* a [[Class]], it's used as the new class's superclass, and all its methods
* are inherited. Otherwise, any arguments passed are treated as objects,
* and their methods are copied over ("mixed in") as instance methods of the
* new class. In cases of method name overlap, later arguments take
* precedence over earlier arguments.
*
* If a subclass overrides an instance method declared in a superclass, the
* subclass's method can still access the original method. To do so, declare
* the subclass's method as normal, but insert `$super` as the first
* argument. This makes `$super` available as a method for use within the
* function.
*
* To extend a class after it has been defined, use [[Class#addMethods]].
*
* For details, see the
* [inheritance tutorial](http://prototypejs.org/learn/class-inheritance)
* on the Prototype website.
**/
function subclass() {};
function create() {
var parent = null, properties = $A(arguments);
if (Object.isFunction(properties[0]))
parent = properties.shift();
function klass() {
this.initialize.apply(this, arguments);
}
Object.extend(klass, Class.Methods);
klass.superclass = parent;
klass.subclasses = [];
if (parent) {
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
parent.subclasses.push(klass);
}
for (var i = 0, length = properties.length; i < length; i++)
klass.addMethods(properties[i]);
if (!klass.prototype.initialize)
klass.prototype.initialize = Prototype.emptyFunction;
klass.prototype.constructor = klass;
return klass;
}
/**
* Class#addMethods(methods) -> Class
* - methods (Object): The methods to add to the class.
*
* Adds methods to an existing class.
*
* [[Class#addMethods]] is a method available on classes that have been
* defined with [[Class.create]]. It can be used to add new instance methods
* to that class, or overwrite existing methods, after the class has been
* defined.
*
* New methods propagate down the inheritance chain. If the class has
* subclasses, those subclasses will receive the new methods — even in
* the context of `$super` calls. The new methods also propagate to instances
* of the class and of all its subclasses, even those that have already been
* instantiated.
*
* ##### Examples
*
* var Animal = Class.create({
* initialize: function(name, sound) {
* this.name = name;
* this.sound = sound;
* },
*
* speak: function() {
* alert(this.name + " says: " + this.sound + "!");
* }
* });
*
* // subclassing Animal
* var Snake = Class.create(Animal, {
* initialize: function($super, name) {
* $super(name, 'hissssssssss');
* }
* });
*
* var ringneck = new Snake("Ringneck");
* ringneck.speak();
*
* //-> alerts "Ringneck says: hissssssss!"
*
* // adding Snake#speak (with a supercall)
* Snake.addMethods({
* speak: function($super) {
* $super();
* alert("You should probably run. He looks really mad.");
* }
* });
*
* ringneck.speak();
* //-> alerts "Ringneck says: hissssssss!"
* //-> alerts "You should probably run. He looks really mad."
*
* // redefining Animal#speak
* Animal.addMethods({
* speak: function() {
* alert(this.name + 'snarls: ' + this.sound + '!');
* }
* });
*
* ringneck.speak();
* //-> alerts "Ringneck snarls: hissssssss!"
* //-> alerts "You should probably run. He looks really mad."
**/
function addMethods(source) {
var ancestor = this.superclass && this.superclass.prototype,
properties = Object.keys(source);
for (var i = 0, length = properties.length; i < length; i++) {
var property = properties[i], value = source[property];
if (ancestor && Object.isFunction(value) &&
value.argumentNames()[0] == "$super") {
var method = value;
value = (function(m) {
return function() { return ancestor[m].apply(this, arguments); };
})(property).wrap(method);
// We used to use `bind` to ensure that `toString` and `valueOf`
// methods were called in the proper context, but now that we're
// relying on native bind and/or an existing polyfill, we can't rely
// on the nuanced behavior of whatever `bind` implementation is on
// the page.
//
// MDC's polyfill, for instance, doesn't like binding functions that
// haven't got a `prototype` property defined.
value.valueOf = (function(method) {
return function() { return method.valueOf.call(method); };
})(method);
value.toString = (function(method) {
return function() { return method.toString.call(method); };
})(method);
}
this.prototype[property] = value;
}
return this;
}
return {
create: create,
Methods: {
addMethods: addMethods
}
};
})();