-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.js
104 lines (90 loc) · 2.32 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var Class = {
prototype: {
init: function() {}
},
extend: function(className, include, implement) {
var object = Object.create(this);
object.parent = this;
object.prototype = Object.create(this.prototype);
if(!className) throw new Error("Class name must be specified");
Object.defineProperty(object, "className", {
get: function() {
return className;
}
});
Object.defineProperty(object.prototype, "getClass", {
value: (function(parentClass) {
return function() {
return parentClass;
}
}(object))
});
/*
var isStrictSupported = (function () { "use strict"; return !this; })();
console.log("Strict Supported: " + isStrictSupported);
*/
if (include)
object.include(include);
if (implement)
object.implement(implement);
return object;
},
create: function() {
var instance = Object.create(this.prototype);
instance.parent = this.parent.prototype;
instance.init.apply(instance, arguments);
return instance;
},
include: function(obj) {
function functionWrapper(key, obj) {
return function () {
var originalSuper = this._super;
this._super = this.parent[key];
var ret = obj[key].apply(this, arguments);
this._super = originalSuper;
return ret;
}
}
for(var key in obj) {
if(typeof obj[key] === "function")
this.prototype[key] = functionWrapper(key, obj);
else
this.prototype[key] = obj[key];
}
return this;
},
implement: function(obj) {
for(var key in obj) {
if(key == "prototype")
this.include(obj[key]);
else this[key] = obj[key];
}
return this;
}
};
Object.defineProperty(Class, "className", {
get: function() {
return "Class";
}
});
Object.defineProperty(Class.prototype, "getClass", {
value: function() {
return Class;
}
});
Object.defineProperty(Class.prototype, "instanceOf", {
value: function(object) {
return object.prototype.isPrototypeOf(this);
}
});
(function () { // For Node.js & Browser compatibility
var root = this;
var _ = new Object();
var isNode = false;
if (typeof module !== 'undefined' && module.exports) {
module.exports = Class;
root.Class = Class;
isNode = true;
}
else root.Class = Class;
})();