-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.js
78 lines (65 loc) · 2.11 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
/*
* JavaScript Inheritance
* Copyright (c) 2009 Ali Farhadi (http://farhadi.ir/)
*
* Released under the terms of the GNU General Public License.
* See the GPL for details (http://www.gnu.org/licenses/gpl.html).
*
* Based on John Resig's Simple JavaScript Inheritance
*/
(function(){
var initializing = false;
// The base Class implementation (does nothing)
this.Class = function(){};
// Create a new Class that inherits from this class
Class.extend = function(prop) {
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
prototype[name] = prop[name];
}
// The dummy class constructor
function Class() {
if (!initializing) {
if (!(this instanceof Class)) {
//When a class is used as a function, self function will be called (if defined)
return Class.self ? Class.self.apply(this, arguments) : null;
}
var _this = this;
var parent = this.parent = {};
var parentClass = Class.parent;
// Creating parent methods
while (true) {
for (key in parentClass.prototype) if (typeof parentClass.prototype[key] == 'function') {
parent[key] = (function(key, parentClass){
return function(){
var tmp = _this.parent;
_this.parent = _this.parent.parent;
parentClass.prototype[key].apply(_this, arguments);
_this.parent = tmp;
};
})(key, parentClass);
}
if (!parentClass.parent) break;
parentClass = parentClass.parent;
parent = parent.parent = {};
}
// Enforce the constructor to be what we expect
this.constructor = Class;
// All construction is actually done in the init method
if (this.init) this.init.apply(this, arguments);
}
}
// Preserve parent class
Class.parent = this;
// Populate our constructed prototype object
Class.prototype = prototype;
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
})();