forked from maccman/juggernaut
/
superclass.js
57 lines (47 loc) · 1.26 KB
/
superclass.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
var JUtils = require("jutils");
SuperClass = module.exports = function(parent){
var result = function(){
this.init.apply(this, arguments);
};
result.prototype.init = function(){};
if (parent){
for(var i in parent){
result[i] = SuperClass.clone(parent[i]);
}
for(var i in parent.prototype){
result.prototype[i] = SuperClass.clone(parent.prototype[i]);
}
result._super = parent;
result.prototype._super = parent.prototype;
}
result.fn = result.prototype;
result.extend = function(obj){
var extended = obj.extended;
for(var i in obj){
result[i] = obj[i];
}
if (extended) extended(result)
};
result.include = function(obj){
var included = obj.included;
for(var i in obj){
result.fn[i] = obj[i];
}
if (included) included(result)
};
result.proxy = function(func){
var thisObject = this;
return(function(){
return func.apply(thisObject, arguments);
});
}
result.fn.proxy = result.proxy;
result.fn._class = result;
return result;
};
SuperClass.clone = function(obj){
if (typeof obj == "function") return obj;
if (typeof obj != "object") return obj;
if (JUtils.isArray(obj)) return JUtils.extend([], obj);
return JUtils.extend({}, obj);
};