-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.js
132 lines (122 loc) · 2.9 KB
/
inheritance.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
Object.create =
Object.create ||
function (obj) {
function F() {}
F.prototype = obj;
return new F();
};
function validateCtor(Ctor) {
if (typeof Ctor !== 'function') {
throw new Error('Ctor must be a function');
}
}
function setMethods(target, ...methods) {
methods.forEach(function (method) {
if (typeof method === 'function' && method.name) {
if (typeof target === 'object') {
target[method.name] = method;
} else if (typeof target === 'function') {
target.prototype[method.name] = method;
}
}
});
}
// 原形链继承
// 返回子类构造函数
function extendsByPrototypeChaining(
SuperType,
superArgs = [],
Ctor = function () {},
...methods
) {
validateCtor(Ctor);
Ctor.prototype = new SuperType(...superArgs);
setMethods(Ctor, ...methods);
return Ctor;
}
// 借用构造函数
// 返回子类构造函数
function extendsByConstructorStealing(
SuperType,
superArgs = [],
Ctor = function () {},
...methods
) {
validateCtor(Ctor);
const _Ctor = Ctor;
Ctor = function (...subArgs) {
SuperType.apply(this, superArgs.concat(subArgs));
_Ctor.call(this, ...subArgs);
};
setMethods(Ctor, ...methods);
return Ctor;
}
// 组合继承
// 返回子类构造函数
function extendsByCombinationInheritance(
SuperType,
superArgs = [],
Ctor = function () {},
...methods
) {
validateCtor(Ctor);
const _Ctor = Ctor;
Ctor = function (...subArgs) {
SuperType.apply(this, superArgs.concat(subArgs));
_Ctor.call(this, ...subArgs);
};
Ctor.prototype = new SuperType();
setMethods(Ctor, ...methods);
return Ctor;
}
// 原型式继承
// 直接返回子类实例
function extendsByPrototypalInheritance(patternObj, props = {}, ...methods) {
const instance = Object.create(patternObj);
Object.keys(props).forEach(function (key) {
instance[key] = props[key];
});
setMethods(instance, ...methods);
return instance;
}
// 寄生式继承
// 直接返回子类实例
function extendsByParasiticInheritance(
patternObj,
enhancer,
props = {},
...methods
) {
function create() {
const clone = Object.create(patternObj);
typeof enhancer === 'function' && enhancer(clone);
return clone;
}
const instance = create();
Object.keys(props).forEach(function (key) {
instance[key] = props[key];
});
setMethods(instance, ...methods);
return instance;
}
// 寄生组合式继承
// 返回子类构造函数
function extendsByParasiticCombinationInheritance(
SuperType,
superArgs = [],
Ctor = function () {},
...methods
) {
validateCtor(Ctor);
const _Ctor = Ctor;
Ctor = function (...subArgs) {
SuperType.apply(this, superArgs.concat(subArgs));
_Ctor.call(this, ...subArgs);
};
// inherit super prototype only
const prototype = Object.create(SuperType.prototype);
prototype.constructor = Ctor;
Ctor.prototype = prototype;
setMethods(Ctor, ...methods);
return Ctor;
}