-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathklass.js
304 lines (271 loc) · 9.26 KB
/
klass.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(function() {
var global = (function(){
return this || (0, eval)('this');
}()),
slice = Array.prototype.slice,
enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'constructor'],
noArgs = [],
TemplateClass = function() {},
chain = function(object) {
TemplateClass.prototype = object;
var result = new TemplateClass();
TemplateClass.prototype = null;
return result;
},
apply = function(object, config) {
if (object && config && typeof config === 'object') {
var i, j, k;
for (i in config) {
object[i] = config[i];
}
if (enumerables) {
for (j = enumerables.length; j--;) {
k = enumerables[j];
if (config.hasOwnProperty(k)) {
object[k] = config[k];
}
}
}
}
};
/**
* Class基类,使用Klass.define()方法声明类继承的顶级父类
*/
var Base = function() {};
apply(Base, {
$isClass: true,
extend: function(SuperClass) {
var superPrototype = SuperClass.prototype,
basePrototype, prototype, name;
prototype = this.prototype = chain(superPrototype);
this.superclass = prototype.superclass = superPrototype;
if (!SuperClass.$isClass) {
basePrototype = Base.prototype;
for (name in basePrototype) {
if (name in prototype) {
prototype[name] = basePrototype[name];
}
}
}
},
/**
* 新增或重写一个static属性
*
* var MyCls = Klass.define({
* ...
* });
*
* MyCls.addStatics({
* someProperty: 'someValue', // MyCls.someProperty = 'someValue'
* method1: function() { ... }, // MyCls.method1 = function() { ... };
* method2: function() { ... } // MyCls.method2 = function() { ... };
* });
*
* @param {Object} members
* @return {Base} this
* @static
*/
addStatics: function(members) {
var member, name;
for (name in members) {
if (members.hasOwnProperty(name)) {
member = members[name];
this[name] = member;
}
}
return this;
},
addMembers: function(members) {
var prototype = this.prototype,
names = [],
i, ln, name, member;
for (name in members) {
names.push(name);
}
if (enumerables) {
names.push.apply(names, enumerables);
}
for (i = 0,ln = names.length; i < ln; i++) {
name = names[i];
if (members.hasOwnProperty(name)) {
member = members[name];
if (typeof member == 'function' && !member.$isClass) {
member.$owner = this;
member.$name = name;
}
prototype[name] = member;
}
}
return this;
},
/**
* 重写类的属性或方法,例子:
* <code>
* var Cls1 = Klass.define({
* constructor: function(name) {
* this.name = name;
* },
*
* say: function() {
* alert(this.name + ' say: hello, world!');
* }
* });
*
* Cls1.implement({
* say: function() {
* alert(this.name + ' say: hello, I'm Max, nice to meet you!');
* },
*
* sayHello: function() {
* alert('hello, world!');
* }
* });
*
* var cls1 = new Cls1();
* cls1.say(); // 输出 'Max say: hello, I'm Max, nice to meet you!'
* cls1.sayHello(); // 输出 'hello world!'
* </code>
*
* 如果想为类的方法定义一个新的别名,应该使用下面的方式,不能使用implement函数:
* <code>
* Cls1.prototype.speak = Cls1.prototype.say;
*
* var cls1 = new Cls1();
* cls1.speak(); // 输出 'Max say: hello, I'm Max, nice to meet you!'
* </code>
*
* @param {Object} overrides 被添加到类的属性或方法
* @static
*/
implement: function() {
this.addMembers.apply(this, arguments);
}
});
// Base类的prototype属性
apply(Base.prototype, {
$isInstance: true,
/**
* 调用当前方法的父类方法,例子:
* <code>
* var Cls1 = Klass.define({
* constructor: function(name) {
* this.name = name;
* },
*
* say: function() {
* alert(this.name + ' say: hello, world!');
* }
* });
*
* var Cls2 = Klass.define(Cls1, {
* constructor: function() {
* thia.callParent(['Max']); // 调用父类的构造函数
* }
* });
*
* var cls2 = new Cls2();
* cls2.say(); // 输出 'Max say: hello, world!'
* </code>
*
* @param {Array/Arguments} args 传递给父类方法的形参
* @return {Object} 返回父类方法的执行结果
*/
callParent: function(args) {
var method,
superMethod = (method = this.callParent.caller) &&
(method = method.$owner ? method : method.caller) &&
method.$owner.superclass[method.$name];
return superMethod.apply(this, args ? slice.call(args, 0) : noArgs);
},
// Default constructor, simply returns `this`
constructor: function() {
return this;
}
});
var makeCtor = function() {
function constructor() {
return this.constructor.apply(this, arguments) || null;
}
return constructor;
};
var extend = function(newClass, newClassExtend) {
var basePrototype = Base.prototype,
SuperClass, superPrototype, name;
if (newClassExtend && newClassExtend !== Object) {
SuperClass = newClassExtend;
} else {
SuperClass = Base;
}
superPrototype = SuperClass.prototype;
if (!SuperClass.$isClass) {
for (name in basePrototype) {
if (!superPrototype[name]) {
superPrototype[name] = basePrototype[name];
}
}
}
newClass.extend(SuperClass);
};
/**
* 声明类,类的继承,重写类方法
*/
var Klass = {
/**
* 声明一个类,或继承自一个父类,子类拥有父类的所有prototype定义的特性,
* 如未定义extend属性,默认继承BaseKlass类,例子:
* <code>
* var Cls1 = Klass.define({
* constructor: function(name) {
* this.name = name;
* },
*
* say: function() {
* alert(this.name + ' say: hello, world!');
* }
* });
*
* var Cls2 = Klass.define(Cls1, {
* constructor: function() {
* thia.callParent(['Max']); // 调用父类的构造函数
* }
* });
*
* var cls2 = new Cls2();
* cls2.say(); // 输出 'Max say: hello, world!'
* </code>
*
* @param {Object} newClassExtend 继承父类
* @param {Object} overrides 类的属性和方法
* @return {Klass} The new class
*/
define: function(newClassExtend, overrides) {
var newClass, name;
if (!newClassExtend && !overrides) {
newClassExtend = Base;
overrides = {};
} else if (!overrides) {
overrides = newClassExtend;
newClassExtend = Base;
}
newClass = makeCtor();
for (name in Base) {
newClass[name] = Base[name];
}
if (overrides.statics) {
newClass.addStatics(overrides.statics);
delete overrides.statics;
}
extend(newClass, newClassExtend);
newClass.addMembers(overrides);
return newClass;
}
};
if (typeof module === 'object' && module && typeof module.exports === 'object') {
module.exports = Klass;
} else if (typeof define === 'function') {
define('klass', [], function() { return Klass; });
}
if (typeof global === 'object' && typeof global.document === 'object') {
global.Klass = Klass;
}
}());