-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.js
More file actions
211 lines (183 loc) · 5.22 KB
/
Function.js
File metadata and controls
211 lines (183 loc) · 5.22 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"use strict";
Object.defineProperty( Function.prototype, 'define', {
value: function ( prototype ) {
var proto = this.prototype;
for ( var i in prototype ) {
Object.defineProperty( proto, i, { value: prototype[ i ], writable: true } );
}
return this;
},
writable: true
} );
Object.defineProperty( Function.prototype, 'static', {
value: function ( prototype ) {
for ( var i in prototype ) {
Object.defineProperty( this, i, { value: prototype[i], writable: true } );
}
return this;
},
writable: true
} );
Object.defineProperty( Function.prototype, 'extend', {
value: function ( parent, prototype ) {
this.prototype = Object.create( parent.prototype );
this.define( prototype );
return this;
},
writable: true
} );
function ResolveMixins ( resolve ) {
if ( !(this instanceof ResolveMixins) ) {
return new ResolveMixins( resolve );
}
this.merge( resolve );
}
(function () {
var proto__implements = new WeakMap();
var obj_checkImplementation = function ( proto, clas ) {
var __implements = proto__implements.get( proto );
if ( __implements ) {
return __implements.indexOf( clas ) >= 0;
}
return false;
};
Object.defineProperty( Object, 'instanceof', {
value: function ( object, clas ) {
return object instanceof Object && object.instanceof( clas );
},
writable: true
} );
Object.defineProperty( Object.prototype, 'instanceof', {
value: function ( clas ) {
// this crashes if not used with functions, can be fixed but not worth the performance cost
if ( this instanceof clas ) {
return true;
}
var proto = Object.getPrototypeOf( this );
do {
if ( obj_checkImplementation( proto, clas ) ) {
return true;
}
proto = Object.getPrototypeOf( proto );
} while ( proto );
return false;
},
writable: true
} );
var func_checkImplementation = function ( clas, iface ) {
var names = Object.getOwnPropertyNames( iface );
for ( var j = 0, jend = names.length; j < jend; ++j ) {
var name = names[ j ];
if ( iface[ name ] instanceof Function &&
!(clas[ name ] instanceof Function) ) {
throw new Error( 'Method "' + name + '" is not implemented' );
}
}
};
var _markImplemented = function ( obj, proto ) {
var __implements = proto__implements.get( obj.prototype );
if ( __implements === undefined ) {
__implements = [];
proto__implements.set( obj.prototype, __implements );
}
do {
__implements.push( proto );
if ( proto.prototype === undefined ) {
break;
}
proto = Object.getPrototypeOf( proto.prototype );
if ( proto ) {
proto = proto.constructor;
}
} while ( proto );
};
Object.defineProperty( Function.prototype, 'implement', {
value: function () {
for ( var i = 0, iend = arguments.length; i < iend; ++i ) {
var proto = arguments[ i ];
_markImplemented( this, proto );
do {
func_checkImplementation( this.prototype, proto.prototype || proto );
// skip built ins
if ( proto.prototype === undefined ) {
break;
}
proto = Object.getPrototypeOf( proto.prototype );
} while ( proto );
}
},
writable: true
} );
Object.defineProperty( Function.prototype, 'mixin', {
value: function () {
var arglen = arguments.length - 1;
var resolve;
if ( arglen > 0 && arguments[ arglen ] instanceof ResolveMixins ) {
resolve = arguments[ arglen ];
}
else {
++arglen;
}
for ( var i = 0; i < arglen; ++i ) {
var mixin = arguments[ i ];
_markImplemented( this, mixin );
var prototype = mixin.prototype || mixin;
var isProto = ( prototype !== mixin );
var keys = Object.getOwnPropertyNames( prototype );
// for ( var key in prototype ) {
for ( var j = keys.length - 1; j >= 0; --j ) {
var key = keys[ j ];
if ( isProto && key === 'constructor' ) {
continue;
}
var value = undefined;
if ( this.prototype[key] !== undefined ) {
if ( resolve && resolve[key] !== undefined ) {
var preffered = resolve[key];
value = preffered.prototype ? preffered.prototype[ key ] : preffered[ key ];
}
if ( !value ) {
throw new Error( 'Unable to mixin property "' + key + '", it is already defined' );
}
}
else {
value = prototype[ key ];
}
Object.defineProperty( this.prototype, key, { value: value, writable: true } );
}
}
return this;
},
writable: true
} );
})();
(function () {
var slice = Array.prototype.slice;
var concat = Array.prototype.concat;
Object.defineProperty( Function.prototype, 'bindArgsAfter', {
value: function () {
var func = this;
var args = slice.call( arguments );
return function ( ) {
return func.apply( this, arguments.length ? concat.call( slice.call( arguments, 0 ), args ) : args );
};
},
writable: true
} );
Object.defineProperty( Function.prototype, 'bindArgsBefore', {
value: function () {
var func = this;
var args = slice.call( arguments );
return function ( ) {
return func.apply( this, arguments.length ? concat.call( args, slice.call( arguments, 0 ) ) : args );
};
},
writable: true
} );
})();
if ( typeof global !== 'undefined' ) {
global.ResolveMixins = ResolveMixins;
}
else if ( typeof window !== 'undefined' ) {
window.ResolveMixins = ResolveMixins;
}