-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (40 loc) · 1.12 KB
/
index.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
function isFunction(fn) {
return typeof fn === 'function'
}
function proxySuper(superFn, fn) {
return function() {
var tmp = this._super;
this._super = superFn;
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
}
}
function Class() {}
Class.extend = function(protoProps) {
var parent = this, _super = parent.prototype, child;
if (protoProps && protoProps.hasOwnProperty('constructor')) {
child = proxySuper(parent, protoProps.constructor);
delete protoProps.constructor; // remove constructor
} else {
child = function() {
parent.apply(this, arguments);
};
}
var prototype = Object.create(parent.prototype, {
constructor: {
value: child,
enumerable: false,
writable: true,
configurable: true
}
});
for (var name in protoProps) {
prototype[name] = isFunction(protoProps[name]) && isFunction(_super[name]) && /\b_super\b/.test(protoProps[name])
? proxySuper(_super[name], protoProps[name]) : protoProps[name];
}
child.prototype = prototype;
child.extend = Class.extend;
return child;
};
module.exports = Class;