-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class.js
87 lines (70 loc) · 1.67 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
/**
* @author Aaron Clinger - https://github.com/aaronclinger/class.js
*/
(function(scope) {
var classInit = false;
function namespace(space) {
if ( ! space) {
return scope;
}
var parent = scope,
parts = space.split('.'),
length = parts.length,
i = -1,
part;
while (++i < length) {
part = parts[i];
parent = parent[part] = parent[part] || {};
}
return parent;
}
scope.createClass = function(options) {
if (typeof options === 'function') {
options = {define: options};
}
var Define = options.define,
Extend = options.extend || null,
type = options.type || '',
name = options.name || Define.toString().match(/function ([^(]+)/)[1],
space = namespace(options.namespace);
var Child = function() {
var child = (typeof Define === 'function') ? new Define() : Define,
isInit = classInit,
override,
parent,
i;
if (Extend) {
classInit = true;
parent = new Extend();
classInit = isInit;
override = function(child, parent) {
return function() {
this.parent = parent;
return child.apply(this, arguments);
};
};
for (i in parent) {
if (typeof parent[i] === 'function') {
if (child[i]) {
child[i] = override(child[i], parent[i]);
} else {
child[i] = parent[i];
}
}
}
}
if ( ! classInit && child.init) {
child.init.apply(child, arguments);
}
return child;
};
switch (type.toLowerCase()) {
case 'static' :
space[name] = new Child();
break;
default :
space[name] = Child;
break;
}
};
}(window));