-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
$.def.js
47 lines (47 loc) · 1.6 KB
/
$.def.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
var global = require('./$.global')
, core = require('./$.core')
, PROTOTYPE = 'prototype';
var ctx = function(fn, that){
return function(){
return fn.apply(that, arguments);
};
};
var $def = function(type, name, source){
var key, own, out, exp
, isGlobal = type & $def.G
, isProto = type & $def.P
, target = isGlobal ? global : type & $def.S
? global[name] : (global[name] || {})[PROTOTYPE]
, exports = isGlobal ? core : core[name] || (core[name] = {});
if(isGlobal)source = name;
for(key in source){
// contains in native
own = !(type & $def.F) && target && key in target;
if(own && key in exports)continue;
// export native or passed
out = own ? target[key] : source[key];
// prevent global pollution for namespaces
if(isGlobal && typeof target[key] != 'function')exp = source[key];
// bind timers to global for call from export context
else if(type & $def.B && own)exp = ctx(out, global);
// wrap global constructors for prevent change them in library
else if(type & $def.W && target[key] == out)!function(C){
exp = function(param){
return this instanceof C ? new C(param) : C(param);
};
exp[PROTOTYPE] = C[PROTOTYPE];
}(out);
else exp = isProto && typeof out == 'function' ? ctx(Function.call, out) : out;
// export
exports[key] = exp;
if(isProto)(exports[PROTOTYPE] || (exports[PROTOTYPE] = {}))[key] = out;
}
};
// type bitmap
$def.F = 1; // forced
$def.G = 2; // global
$def.S = 4; // static
$def.P = 8; // proto
$def.B = 16; // bind
$def.W = 32; // wrap
module.exports = $def;