-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass.js
More file actions
executable file
·277 lines (236 loc) · 5.56 KB
/
Copy pathClass.js
File metadata and controls
executable file
·277 lines (236 loc) · 5.56 KB
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
/*
* new a interface
* sample:
* var sampleInterface = new Interface({
* 'getName':null,
* 'sample' : 'string',
* 'sample2' : ['string', 'array']
* });
*/
function Interface(it) {
if(typeof it === 'undefined') {
throw 'no interface asignment!';
}
return it;
}
/*
* a function to create class
* sample:
* var SampleClass = new Class(function SampleClass() {
* alert('hello world');
* });
*
* var concrete = new SampleClass(); //nothing happened here!
* concrete.init(); //alert('hello world');
*/
var Class = function(name, cl) {
return Class.create(name, cl);
}
/*
* record all the name of the class create by Class
*/
Class.classes = [];
/*
* equal new Class(constructor)
*/
Class.create = function(className, func) {
if(arguments.length < 2) {
throw 'you need asign a class name and a constructor!'
}
if(typeof func !== 'function')
throw className + ' need a constructor!';
if(func.name !== '')
throw 'constructor must a anonymous function!'
/*
* t is a temp class object
*/
function t() {
return t.parents[0];
}
t._name = className;
Class.classes.push(t._name);
/*
* record all the parents class include itself
*/
t.parents = [];
t.parents[t.parents.length] = new t;
/*
* bind the constructor to class t, it's conversion for
* child class to call the parents's constructor
*/
t.init = func;
/*
* bind the constructor to the object of the class t
*/
t.prototype.init = func;
t._this = t.parents[0];
/*
* seter
* */
t.prototype.set = function(name, value) {
this[name] = value;
return this[name];
}
/*
* geter
* */
t.prototype.get = function(name) {
var temp = this;
for(key in this)
if(name === key)
return temp[name];
if(temp = temp.getClass().getParent())
return temp.get(temp);
return false;
}
/*
* to get the constructor name
* so the constructor need a name same with the class var
*/
t.__getName = function () {
return t._name;
}
/*
* t is a class object, the method method
* add method to the object
* sample:
* var SampleClass = new Class(function SampleClass () {} );
* SampleClass.method('setName' function(name) {
* this.name = name;
* return this;
* });
*/
t.method = function (name, func) {
t.prototype[name] = func;
return this;
}
/*
* the function to find the right prototype method for use
*/
t.prototype.run = function (name) {
for(var i = 0; i < t.parents.length; ++i) {
if(typeof t.parents[i][name] !== 'undefined')
return t.parents[i][name];
}
if(typeof t.parents[1] !== 'undefined' && t.parents[1].getClass().parents[1] !== 'undefined')
return t.parents[1].run(name);
throw 'no ' + name + ' exist in ' + t._name;
}
/*
* extend prototype method from parent
* sample:
* var Parent = new Class(function Parent() {});
* Parent.method('setName', function(name) {
* this.name = name;
* return this;
* });
* var Child = new Class(function Child() {
* Parent.init(); //every Child need do this to construct Parent in Child
* ...
* });
* Child.extend(Parent);
* var child = new Child();
* child.setName('hello');
*
*/
t.extend = function (parent) {
t.parents[t.parents.length] = new parent;
}
/*
* implement a Interface
* sample:
* var SampleInterface = new Interface({
* 'setName' : 'string',
* 'getName' : null,
* 'sayHello' : null
* });
* var SampleClass = new Class(function SampleClass() {});
* SampleClass.method('setName' function(name) {
* this.name = name;
* });
*
* SampleClass.implement(SampleInterface); > will throw a uncapture exception because getName, sayHello not exist in SampleClass
*/
t.hasParents = function() {
return t.parents.length == 1 ? false : true;
}
t.getParent = function() {
return t.hasParents() ? t.parents[1] : false;
}
t.implement = function(interf) {
if(typeof interf != 'object') {
throw 'interface type error';
}
var cs = [];
var temp = t;
do {
for(key in temp.prototype)
cs.push(key);
temp = temp.getParent();
if(temp != false) {
temp = temp.getClass();
}
} while(temp);
var it = [];
for(key in interf)
it.push(key);
var al = '';
for(var i = 0; i < it.length; ++i) {
for(var j = 0; j < cs.length; ++j)
if(it[i]===cs[j])
break;
if(j === cs.length)
al += it[i] + ',';
}
if(al !== '') {
al += ' need implement in ' + t._name;
throw al;
}
}
t.prototype.getClass = function() {
return t;
}
return t;
}
/*
* return how many class ready to use
*/
Class.number = function() {
return Class.classes.length;
};
/*
* get all the class name
*/
Class.getAll = function() {
return Class.classes;
}
/*
* check if the class created by Class
*/
Class.exist = function(name) {
if(name === null)
return false;
if(typeof name === 'function')
name = name.__getName();
for(var i = 0; i < Class.number(); ++i) {
if(Class.getAll()[i] == name)
return true;
}
return false;
}
Array.prototype.search = function(value) {
for(var i = 0; i < this.length; ++i)
if(this[i] === value)
return i;
return false;
}
Array.prototype.del = function(value) {
var pos = this.search(value);
for(var i = pos; i < this.length - 1; ++i)
this[i] = this[i + 1];
this.length -= 1;
}
var T = new Class('T', function() {});
T.method('hello', function() {
alert('hello');
});