/
index.js
226 lines (179 loc) · 5.69 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
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
/**
* 类的继承
* @author ydr.me
* @create 2014年10月04日15:09:02
* @update 2016年04月22日01:41:01
* @update 2016年11月23日11:23:54
*/
'use strict';
var access = require('blear.utils.access');
var typeis = require('blear.utils.typeis');
var object = require('blear.utils.object');
var random = require('blear.utils.random');
var debug = require('blear.utils.debug');
var classId = 0;
// var rePublicKey = /^[a-zA-Z_][a-zA-Z\d]*$/;
// var CONSTRUCTOR_NAME = 'constructor';
var CLASSIFY_NAME = '__classify__';
// var reParentCall = /\.parent\(/;
var makeExtend = function (superClass) {
if (superClass[CLASSIFY_NAME] && superClass.extend) {
return superClass.extend;
}
superClass[CLASSIFY_NAME] = true;
superClass.classId = superClass.prototype.classId
= superClass.classId || superClass.prototype.classId || classId++;
superClass.className = superClass.prototype.className;
return function extend(prototype) {
if (!prototype) {
throw new SyntaxError('原型链不能为空');
}
if (!typeis.Object(prototype) && !typeis.Function(prototype)) {
throw new SyntaxError('原型链必须为一个 Object/Function 实例');
}
if (typeis.Function(prototype)) {
prototype = {
constructor: prototype
};
}
var ChildClass = prototype.constructor;
if (!typeis.Function(ChildClass) || ChildClass === Object) {
throw new SyntaxError('原型链的构造函数不能为空');
}
/**
* 类的原型继承
* @type {Object}
*/
ChildClass.prototype = Object.create(superClass.prototype);
/**
* 类的 ID
* @type {number}
*/
ChildClass.classId = prototype.classId = classId++;
/**
* 类的名称
* @type {*|string}
*/
ChildClass.className = prototype.className;
/**
* 当前类的父类指向
* @type {exports}
*/
ChildClass.superClass = prototype.superClass = superClass;
/**
* 子类原型
* @type {ChildClass}
*/
prototype.constructor = ChildClass;
/**
* 生成类唯一识别号,用于创建保护属性名
* @returns {string}
*/
ChildClass.sole = function sole() {
return '_' + ChildClass.className + '_' + random.guid();
};
/**
* 调用父级的构造函数
* @param instance {Object} 子级实例
*/
ChildClass.parent = function (instance/*arguments*/) {
if (!instance || !instance.classId) {
throw new SyntaxError('调用父级构造方法时,必须传递当前实例。');
}
var args = access.args(arguments).slice(1);
superClass.apply(instance, args);
// 向上传递错误实例的错误堆栈
try {
Error.captureStackTrace(instance, ChildClass);
} catch (err) {
// ignore
}
};
/**
* 调用祖先原型方法
* @param method {String} 方法名称
* @param instance {Object} 实例
* @returns {*}
*/
ChildClass.invoke = function (method, instance/*arguments*/) {
// 这里会自动向祖先查找原型方法
var fn = superClass.prototype[method];
if (!typeis.Function(fn)) {
throw new ReferenceError('未找到祖先的`' + method + '`原型方法');
}
var args = access.args(arguments).slice(2);
return fn.apply(instance, args)
};
ChildClass.superInvoke = debug.deprecate(ChildClass.invoke, '使用 `.invoke` 方法代替');
/**
* 原来的原型
*/
object.each(prototype, function (protoKey, protoVal) {
ChildClass.prototype[protoKey] = protoVal;
});
/**
* 子类的继承方法
* @type {Function}
*/
ChildClass.extend = makeExtend(ChildClass);
return ChildClass;
};
};
/**
* Class 就是一个根类,所有基于 Class 扩展出来的类都具有以下静态方法
*
* # 静态方法
* - `.sole` 生成唯一的随机值,用于原型方法
* - `.alias` 原型方法的别名
* - `.extend` 基于当前类扩展新的子类
* # 实例方法
* **只能同步调用**
* - `this.Super()` 执行父类构造函数,需要手动执行
* - `this.Super.someMethod()` 执行祖先类的原型方法
*
* @class Class
*/
var Class = function Class() {
// Class constructor
};
Class.classId = classId++;
Class.className = 'Class';
Class.superClass = null;
Class.prototype = {
constructor: Class,
classId: Class.classId,
className: Class.className,
superClass: Class.superClass
};
/**
* Class 化
* @param constructor {Function} 构造函数
* @returns {*}
*/
Class.ify = function (constructor) {
if (!typeis.Function(constructor)) {
throw new SyntaxError('ify constructor 必须是构造函数');
}
constructor.extend = makeExtend(constructor);
return constructor;
};
/**
* 判断是否为 Class 扩展类
* @param constructor
* @returns {boolean}
*/
Class.is = function (constructor) {
if (!typeis.Function(constructor)) {
return false;
}
return Boolean(constructor[CLASSIFY_NAME]);
};
/**
* 类的继承
* @param prototype {Object} 原型链
* @param prototype.constructor {Function} 构造函数
* @param [prototype.className="anonymous"] {String} 类的命令
* @returns {constructor}
*/
Class.extend = makeExtend(Class);
module.exports = Class;