-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.js
190 lines (167 loc) · 4.39 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
180
181
182
183
184
185
186
187
188
189
190
/**
* 类
*
* @module Class
*/
'use strict';
var inherit = function(Child, Parent) {
// 不使用`new Parent()`,以避免引入非原型方法/属性
var Bridge = function() {};
Bridge.prototype = Parent.prototype;
Child.prototype = new Bridge();
Child.superclass = Parent.prototype;
Child.prototype.constructor = Child;
};
var slice = Array.prototype.slice,
unshift = Array.prototype.unshift;
var mixin = function() {
var args = slice.call(arguments, 0),
target = args.shift(),
source, p;
while ((source = args.shift())) {
for (p in source) {
// 保留属性 mixins
if (p === 'mixins') {
source[p].unshift(target);
mixin.apply(null, source[p]);
} else {
target[p] = source[p];
}
}
}
};
/**
* 类,简单 OO 实现
*
* @class Class
*/
var Class = function() {};
Class.superclass = Class.prototype = {
// constructor: Class,
/**
* 初始化方法,实例化时自动执行
*
* @method initialize
*/
initialize: function() {},
/**
* 扩展实例方法/属性
* @method extend
* @param {Object} obj1 实例方法集
* @param {Object} [objN] 实例方法集
* @return {Object} 类实例
*
* @example
* ```
* var bob = new Person('Bob', 13);
*
* bob.extend({
* say: function () {
* console.log('My name is ' + this.name + '.');
* console.log('I\'m ' + this.age + ' years old.');
* }
* });
*
* bob.say();
* // My name is Bob.
* // I'm 13 years old.
* ```
*/
extend: function( /*obj1[, objN]*/ ) {
Array.prototype.unshift.call(arguments, this);
mixin.apply(null, arguments);
return this;
}
};
/**
* 创建类
* @constructor
* @method create
* @static
* @param {Function} [Brood] 将要继承的父类(只继承其原型方法)
* @param {Object} [Proto] 将要扩展的实例方法集
* @param {Object} [ProtoN] 将要扩展的实例方法集
* @return {Function} 类
*
* @example
* ```
* // 创建 `Person` 类
* var Person = Class.create({
* initialize: function (name, age) {
* this.name = name;
* this.age = age;
* }
* });
*
* // 创建 `Student` 类,不推荐采用这种方法;
* // 推荐采用下文的静态方法 `SomeSuperClass.extend`
* var Student = Class.create(Person, {
* initialize: function (name, age, school) {
* // `Student` 的 `superclass` 是 `Person`
* Student.superclass.initialize.apply(this, arguments);
* this.school = school;
* }
* });
*
* var jack = new Person('Jack', 34);
* // now:
* // jack.name === 'Tom';
* // jack.age === 34;
* // jack.school === undefined;
*
* var tom = new Student('Tom', 21, 'MIT');
* // now:
* // tom.name === 'Tom';
* // tom.age === 21;
* // tom.school === 'MIT';
* ```
*/
Class.create = function( /*[Brood][, Proto[, ProtoN]]*/ ) {
var args = slice.call(arguments, 0),
Dummy,
Brood;
Dummy = function() {
this.initialize.apply(this, arguments);
};
if (args[0] && typeof args[0] === 'function') {
Brood = args.shift();
// make sure Classes inherited from Class or Class's sub-classes
if (!Brood.superclass) {
inherit(Brood, Class);
}
} else {
Brood = Class;
}
inherit(Dummy, Brood);
if (args.length) {
args.unshift(Dummy.prototype);
mixin.apply(null, args);
}
/**
* 扩展类
*
* @method extend
* @static
* @param {Object} [Proto] 将要扩展的实例方法集
* @param {Object} [ProtoN] 将要扩展的实例方法集
* @return {Function} 类
*
* @example
* ```
* // 创建 `Student` 类,从 `Persion` 类扩展
* var Student = Person.extend({
* initialize: function (name, age, school) {
* // 调用父类的初始化方法,根据业务自由决定是否需要
* Student.superclass.initialize.apply(this, arguments);
* this.school = school;
* }
* });
* ```
*/
Dummy.extend = function( /*[Proto[, ProtoN]]*/ ) {
unshift.call(arguments, Dummy);
return Class.create.apply(null, arguments);
};
return Dummy;
};
module.exports = Class;