- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.js
More file actions
Latest commit
139 lines (128 loc) · 3.83 KB
/
Copy pathBase.js
File metadata and controls
139 lines (128 loc) · 3.83 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Base.js, version 1.1a
Copyright 2006-2010, Dean Edwards
License: http://www.opensource.org/licenses/mit-license.php
*/
varBase=function(){
// dummy
};
Base.extend=function(_instance,_static){// subclass
varextend=Base.prototype.extend;
// build the prototype
Base._prototyping=true;
varproto=newthis;
extend.call(proto,_instance);
proto.base=function(){
// call this method from any other method to invoke that method's ancestor
};
deleteBase._prototyping;
// create the wrapper for the constructor function
//var constructor = proto.constructor.valueOf(); //-dean
varconstructor=proto.constructor;
varklass=proto.constructor=function(){
if(!Base._prototyping){
if(this._constructing||this.constructor==klass){// instantiation
this._constructing=true;
constructor.apply(this,arguments);
deletethis._constructing;
}elseif(arguments[0]!=null){// casting
return(arguments[0].extend||extend).call(arguments[0],proto);
}
}
};
// build the class interface
klass.ancestor=this;
klass.extend=this.extend;
klass.forEach=this.forEach;
klass.implement=this.implement;
klass.prototype=proto;
klass.toString=this.toString;
klass.valueOf=function(type){
//return (type == "object") ? klass : constructor; //-dean
return(type=="object") ? klass : constructor.valueOf();
};
extend.call(klass,_static);
// class initialisation
if(typeofklass.init=="function")klass.init();
returnklass;
};
Base.prototype={
extend: function(source,value){
if(arguments.length>1){// extending with a name/value pair
varancestor=this[source];
if(ancestor&&(typeofvalue=="function")&&// overriding a method?
// the valueOf() comparison is to avoid circular references
(!ancestor.valueOf||ancestor.valueOf()!=value.valueOf())&&
/\bbase\b/.test(value)){
// get the underlying method
varmethod=value.valueOf();
// override
value=function(){
varprevious=this.base||Base.prototype.base;
this.base=ancestor;
varreturnValue=method.apply(this,arguments);
this.base=previous;
returnreturnValue;
};
// point to the underlying method
value.valueOf=function(type){
return(type=="object") ? value : method;
};
value.toString=Base.toString;
}
this[source]=value;
}elseif(source){// extending with an object literal
varextend=Base.prototype.extend;
// if this object has a customised extend method then use it
if(!Base._prototyping&&typeofthis!="function"){
extend=this.extend||extend;
}
varproto={toSource: null};
// do the "toString" and other methods manually
varhidden=["constructor","toString","valueOf"];
// if we are prototyping then include the constructor
vari=Base._prototyping ? 0 : 1;
while(key=hidden[i++]){
if(source[key]!=proto[key]){
extend.call(this,key,source[key]);
}
}
// copy each of the source object's properties to this object
for(varkeyinsource){
if(!proto[key])extend.call(this,key,source[key]);
}
}
returnthis;
}
};
// initialise
Base=Base.extend({
constructor: function(){
this.extend(arguments[0]);
}
},{
ancestor: Object,
version: "1.1",
forEach: function(object,block,context){
for(varkeyinobject){
if(this.prototype[key]===undefined){
block.call(context,object[key],key,object);
}
}
},
implement: function(){
for(vari=0;i<arguments.length;i++){
if(typeofarguments[i]=="function"){
// if it's a function, call it
arguments[i](this.prototype);
}else{
// add the interface using the extend method
this.prototype.extend(arguments[i]);
}
}
returnthis;
},
toString: function(){
returnString(this.valueOf());
}
});