/
class.js
84 lines (65 loc) · 1.74 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
if (typeof Object.create !== "function")
Object.create = function(o) {
function F() {}
F.prototype = o;
return new F();
};
var exports = this;
(function(){
var moduleKeywords = ["included", "extended"];
var Class = {
inherited: function(){},
created: function(){},
prototype: {
initializer: function(){},
init: function(){}
},
create: function(include, extend){
var object = Object.create(this);
object.parent = this;
object.prototype = object.fn = Object.create(this.prototype);
if (include) object.include(include);
if (extend) object.extend(extend);
object.created();
this.inherited(object);
return object;
},
init: function(){
var initance = Object.create(this.prototype);
initance.parent = this;
initance.initializer.apply(initance, arguments);
initance.init.apply(initance, arguments);
return initance;
},
proxy: function(func){
var thisObject = this;
return(function(){
return func.apply(thisObject, arguments);
});
},
proxyAll: function(){
var functions = makeArray(arguments);
for (var i=0; i < functions.length; i++)
this[functions[i]] = this.proxy(this[functions[i]]);
},
include: function(obj){
for(var key in obj)
if (moduleKeywords.indexOf(key) == -1)
this.fn[key] = obj[key];
var included = obj.included;
if (included) included.apply(this);
return this;
},
extend: function(obj){
for(var key in obj)
if (moduleKeywords.indexOf(key) == -1)
this[key] = obj[key];
var extended = obj.extended;
if (extended) extended.apply(this);
return this;
}
};
Class.fn = Class.prototype;
Class.fn.proxy = Class.proxy;
exports.Class = Class;
})();