-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXClass.js
More file actions
387 lines (311 loc) · 10.7 KB
/
XClass.js
File metadata and controls
387 lines (311 loc) · 10.7 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
@module xclass
**/
(function (global) {
'use strict';
var objectEach = function (obj, fn , scope ) {
for (var x in obj)
fn.call( scope, x, obj[x] );
};
var arrayEach = Array.prototype.forEach ? function (array, fn ,scope) {
return array.forEach( fn ,scope )
} : function (obj, fn , scope) {
for (var i = 0 , len = obj && obj.length || 0; i < len; i++)
fn.call( scope , obj[i], i);
};
var wrapFunction = function( func ){
var wrapper = function(){
var lastCaller = this.$caller , rtn;
this.$caller = wrapper;
rtn = func.apply( this , arguments );
this.$caller = lastCaller;
return rtn;
};
wrapper.prototype = func.prototype;
return wrapper;
};
var extend = function (params, notOverridden) {
var me = this;
objectEach(params, function (name, value) {
var prev = me[ name ];
if (prev && notOverridden === true)
return;
if (typeof value === 'function' && name !== 'parent' && !notOverridden ) {
var m = me[ name ] = wrapFunction( value );
m.$name = name;
m.$owner = me;
m.$orgin = value;
if (prev)
m.$prev = prev;
}else{
me[ name ] = value;
}
});
};
var ns = function ( name , root , createAsNeeded) {
var part = root || global,
parts = name && name.split('.') || [];
arrayEach(parts, function (partName) {
if (partName && part) {
part = part[ partName ] || createAsNeeded !== false && ( part[ partName ] = {} );
}
});
return part;
};
/**
Base Class , All classes created by XClass inherit from XNative
@class XNative
@param {Object} params The constructor parameters.
*/
var XNative = function ( params ) {
};
/**
If there is a name for example 'MixinClass', this mixin scope will be this.mixins.MixinClass,
Otherwise the scope will be 'this' ( the mixed-in-instance )
@method mixin
@static
@param {XNative} mixinClass
@param {String} name
@return {XNative} itself
*/
XNative.mixin = function ( mixinClass , name ) {
this.$mixins.push( { name : name , mixin : mixinClass } );
return this;
};
/**
Implement functions to class
@method implement
@static
@param {Object} params
@param {Boolean} safe
@return {XNative} itself
*/
XNative.implement = function ( params , safe ) {
extend.call(this.prototype, params , safe );
return this;
};
/**
constructor function
@method initialize
*/
XNative.prototype.initialize = function () {};
/**
Implement functions to instance
@method implement
@param {Object} params
@param {Boolean} safe
@return {XNative} itself
*/
XNative.prototype.implement = function ( params , safe ) {
extend.call(this, params , safe );
return this;
};
/**
Call super class's function having the same function name
@method parent
*/
XNative.prototype.parent = function () {
var caller = this.$caller ,
func = caller && caller.$prev;
if (!func)
throw new Error('can not call parent');
else {
return func.apply(this, arguments);
}
};
var PROCESSOR = {
'statics':function (newClass, methods) {
objectEach(methods, function (k, v) {
newClass[ k ] = v;
});
},
'extend':function (newClass, superClass) {
superClass = superClass || XNative;
var prototype = newClass.prototype , superPrototype = superClass.prototype;
//process statics
objectEach(superClass, function (k, v) {
if( !( k in newClass ) )
newClass[ k ] = v;
});
//process prototype
newClass.implement( superPrototype );
//process mixins
var mixins = newClass.$mixins = [];
if (superClass.$mixins)
mixins.push.apply( mixins , superClass.$mixins );
newClass.$superclass = prototype.$superclass = superClass;
},
'mixins':function (newClass, value) {
if( value )
arrayEach(value, function (v) {
if( typeof v === 'function')
newClass.mixin( v );
else if( v.mixin )
newClass.mixin( v.mixin , v.name )
});
}
};
var PROCESSOR_KEYS = ['statics', 'extend', 'mixins'];
/**
Example:
new XClass({
statics : {
static_method : function(){}
},
method1 : function(){},
method2 : function(){},
extend : ExtendedClass,
mixins : [ MixedClass1 , MixedClass2 ],
singleton : false
});
@class XClass
@main
@constructor
@param {Object} params
@return {XNative} The new class
*/
function XClass( params ){
params = params || {};
var XNative = function(){
return reset( this , XNative , params , arguments );
};
var methods = {};
arrayEach(PROCESSOR_KEYS, function (key) {
PROCESSOR[ key ](XNative, params[ key ], key);
});
objectEach(params, function (k, v) {
if ( !PROCESSOR.hasOwnProperty( k ) )
methods[ k ] = v;
});
extend.call(XNative.prototype, methods);
return XNative;
}
function reset( instance , XNative , params , args ){
var me = instance;
if ( params.singleton )
if (XNative.$singleton)
return XNative.$singleton;
else
XNative.$singleton = me;
var mixins = XNative.$mixins;
arrayEach( mixins , function (mixin) {
var name = mixin.name ,
mixinClass = mixin.mixin ,
obj = createNewInstance( mixinClass,args );
if( name )
ns( 'mixins' , me )[ name ] = obj;
else
me.implement( obj , true );
});
instance.$constructor = XNative;
return me.initialize && me.initialize.apply(me, args ) || me;
}
function createNewInstance( sourceClass , args ){
// Performance optimization: http://jsperf.com/apply-vs-call-vs-invoke & http://jsperf.com/invoke-new-function-vs-function
var length = args && args.length || 0;
switch ( length ) {
case 0: return new sourceClass();
case 1: return new sourceClass(args[0]);
case 2: return new sourceClass(args[0], args[1]);
case 3: return new sourceClass(args[0], args[1], args[2]);
case 4: return new sourceClass(args[0], args[1], args[2], args[3]);
case 5: return new sourceClass(args[0], args[1], args[2], args[3], args[4]);
case 6: return new sourceClass(args[0], args[1], args[2], args[3], args[4], args[5]);
case 7: return new sourceClass(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
case 8: return new sourceClass(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
case 9: return new sourceClass(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7],
args[8]);
case 10: return new sourceClass(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7],
args[8], args[9]);
default:
var newClass = function(){ return sourceClass.apply( this , args );};
newClass.prototype = sourceClass.prototype;
return new newClass();
}
}
/**
@class utils
@namespace XClass
@static
*/
XClass.utils = {
/**
Iterates through an object and invokes the given callback function for each iteration
@method objectEach
@static
@param {Object} object The object ot iterate
@param {Function} The callback function
*/
objectEach:objectEach,
/**
Iterates an array and invokes the given callback function for each iteration , It will call Array.prototype.forEach if supported
@method arrayEach
@static
@param {Array} object The array ot iterate
@param {Function} The callback function
*/
arrayEach:arrayEach,
/**
Creates namespaces to be used for scoping variables
@method ns
@static
@param {String} name dot-namespaced format namespaces, for example: 'Myapp.package'
@param {Object} root the root object, global if null
@param {Boolean} createAsNeeds create the context if needed, default true
@return {Object} The namespace object, if name is null , it returns the root
*/
ns:ns
};
/**
Define a class
@static
@for XClass
@method define
@param {String} className The class name to create in string dot-namespaced format, for example: 'Myapp.MyClass'
@param {Object} params The parameters for the new Class
@param {Object} root The root, default global
@return {XNative} The XNative Class
*/
XClass.define = function (className, params , root ) {
if (className) {
var lastIndex = className.lastIndexOf('.');
return ns(lastIndex === -1 ? null : className.substr(0, lastIndex) , root )[ className.substr(lastIndex + 1) ] = new XClass(params);
} else
throw new Error('empty class name!');
};
/**
Checks if an object is an instance of a XClass Object.
@static
@for XClass
@method instanceOf
@param {Object} instance the object to check
@param {Object} xNative the XNative Object
@return {Boolean} Whether the object is an instance of the XNative Object.
*/
XClass.instanceOf = function( instance , xNative ){
if( !instance || !instance.parent || !xNative )
return false;
var cls = instance.$constructor;
while(cls){
if( cls === xNative )
return true;
else
cls = cls.$superclass;
}
return false;
};
/**
@property version
@type {String}
*/
XClass.version = "2.0.3";
if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.exports)
module.exports = XClass;
else
global.XClass = XClass;
// amd support
if (typeof define === 'function' && define.amd)
define('xclass', [], function () {
return XClass;
});
})(this);