-
Notifications
You must be signed in to change notification settings - Fork 1
/
zetderiver.js
132 lines (107 loc) · 2.8 KB
/
zetderiver.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
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
;(function(){
var key = {};
function buildChain(child, zuper){
for(var i in zuper){ // parent has less info than child, so, faster
var childFnc = child[i];
if(typeof(childFnc) == "function"
&& i != "inherited"
&& i != "$Protected"
&& i != "$Public"
&& i != "getProtectedScope"
){
//check if this is not the same
if(zuper[i] !== childFnc){
childFnc._superMethod = zuper[i];
}
}
}
}
function cache(obj){
var result = {};
for(var i in obj){
result[i] = obj[i];
}
return result;
}
function setter(to){
return function(proto){
for(var i in proto){
to[i] = proto[i];
}
}
}
var undef;
var g = window.$Declare = function derived(superclass, def){
// $Declare(BlaaClass,function(){}
// this can be the case when someone passes uninited contructor
// helps to prevent such errors
var superklazz;
if(arguments.length == 1){
def = superclass;
}else if(arguments.length == 2){
superklazz = superclass;
}
return function(k){
var instance, scope;
var hasSuper = typeof(superklazz) != "undefined";
var superScope, superThis;
var cacheScope, cacheThis;
if(hasSuper){
superThis = superclass(key);
superScope = superThis.getProtectedScope && superThis.getProtectedScope();
cacheThis = superThis && cache(superThis);
cacheScope = superScope && cache(superScope);
}
scope = (superScope || {
that : null
});
instance = superThis || {};
scope.$Protected = setter(scope);
scope.$Public = setter(instance);
// building object from factory function
var fact = null;
with(scope){
eval("fact = " + def.toString());
}
//discard use of 'this'
fact.call(window);
//putting back instance, so that it's become available to private scope
scope.that = instance;
if(hasSuper){
// making inheritance chain for public
cacheThis && buildChain(instance, cacheThis);
// making inheritance chain for protected
cacheScope && buildChain(scope, cacheScope);
}
instance.inherited = function(newargs, args){
var callee;
if(arguments.length == 1){
callee = newargs.callee;
}else if(arguments.length == 2){
callee = args.callee;
}
if(callee){
//searching for superCall
var method = callee._superMethod;
if(method){
// console.debug('Found method');
method.apply(window,newargs);
}else{
// console.error('Has no super method');
}
}else{
// console.error('Wrong arguments');
}
}
if(k === key){
//dealing with subclasses
instance.getProtectedScope = function(){
return scope;
}
}else if(instance.init){ //normal call
instance.init.apply(window, arguments);
}
return instance;
};
}
})();