This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
221 lines (213 loc) · 6.28 KB
/
index.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
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
212
213
214
215
216
217
218
219
220
221
(function(O, F) {
"use strict";
var O_PROTO = "__proto__";
var P_PROTO = "___proto_polyfill_proto___";
var P_FUNCT = "___proto_polyfill_funct___";
var P_VALUE = "___proto_polyfill_value___";
var SYMBOL = "Symbol(";
var getPrototypeOf = O["getPrototypeOf"];
var getOwnPropertyNames = O["getOwnPropertyNames"];
var defineProperty = O["defineProperty"];
var getOwnPropertyDescriptor = O["getOwnPropertyDescriptor"];
var create = O["create"];
function getFunction(source, name, what) {
var info = getOwnPropertyDescriptor(source, name);
var func = info[what];
return func[P_FUNCT] || func;
}
function prepareFunction(dest, source, name, what) {
function newFunction() {
return getFunction(source, name, what).apply(dest, arguments);
}
defineProperty(newFunction, P_FUNCT, {
get: function pFunctionGet() {
return getFunction(source, name, what);
},
enumerable: false,
configurable: false
});
return newFunction;
}
function setProperty(dest, source, name) {
var info = getOwnPropertyDescriptor(source, name);
var hasSetter = info.set instanceof F;
var hasGetter = info.get instanceof F;
if (hasSetter && hasGetter) {
defineProperty(dest, name, {
set: prepareFunction(dest, source, name, "set"),
get: prepareFunction(dest, source, name, "get"),
enumerable: info.enumerable || false,
configurable: true
});
} else if (hasSetter) {
defineProperty(dest, name, {
set: prepareFunction(dest, source, name, "set"),
enumerable: info.enumerable || false,
configurable: true
});
} else if (hasGetter) {
defineProperty(dest, name, {
get: prepareFunction(dest, source, name, "get"),
enumerable: info.enumerable || false,
configurable: true
});
} else {
defineProperty(dest, name, {
set: function(v) {
this[P_VALUE][name] = v;
},
get: function() {
return name in this[P_VALUE] ? this[P_VALUE][name] : this === dest ? source[name] : dest[name];
},
enumerable: info.enumerable || false,
configurable: true
});
}
}
function setProperties(dest, source) {
var names = getOwnPropertyNames(source),
name,
n = 0;
for (; n < names.length; n++) {
name = names[n];
if (
name &&
name !== O_PROTO &&
name !== P_PROTO &&
name !== P_FUNCT &&
name !== P_VALUE &&
name.indexOf(SYMBOL) !== 0 &&
!dest.hasOwnProperty(name)
) {
setProperty(dest, source, name);
}
}
}
function setProto(dest, source) {
var sourceProto = typeof source === "function" ? source.prototype : source;
var sourceConstructor = typeof source === "function" ? source : sourceProto && sourceProto.constructor;
defineProperty(dest, P_PROTO, {
value: sourceProto || source,
enumerable: false,
configurable: false,
writable: false
});
defineProperty(dest, P_VALUE, {
value: {},
enumerable: false,
configurable: false,
writable: false
});
if (!sourceConstructor) {
return;
}
setProperties(dest, sourceConstructor);
}
if (
!(O_PROTO in O) &&
!(O_PROTO in F) &&
getPrototypeOf instanceof F &&
getOwnPropertyNames instanceof F &&
defineProperty instanceof F &&
getOwnPropertyDescriptor instanceof F
) {
O["setPrototypeOf"] = function oSetPrototypeOf(obj, proto) {
if (obj instanceof O && obj !== null) {
obj.__proto__ = proto;
}
return obj;
};
O["getPrototypeOf"] = function oGetPrototypeOf(obj) {
return obj instanceof O && obj !== null ? obj.__proto__ : getPrototypeOf(obj);
};
defineProperty(O, "create", {
value: function oCreate(source, props) {
var C = create(source, props);
defineProperty(C, O_PROTO, {
get: function cGetProto() {
if (this === C) {
return source;
} else {
return C;
}
},
enumerable: false,
configurable: false
});
return C;
},
enumerable: false,
configurable: true,
writable: true
});
defineProperty(O.prototype, O_PROTO, {
get: function oGetProto() {
switch (typeof this) {
case "string":
return String.prototype;
case "number":
return Number.prototype;
case "boolean":
return Boolean.prototype;
}
if (P_PROTO in this) {
return this[P_PROTO];
}
var constr = this.constructor;
if (!constr) {
return null;
} else if (typeof constr.prototype === "function") {
return constr;
} else if (this instanceof constr) {
return constr.prototype || null;
} else {
var proto = constr.__proto__;
return this !== O.prototype && proto.prototype === undefined ? O.prototype : proto.prototype || null;
}
},
set: function oSetProto(proto) {
if (proto && this instanceof O) {
defineProperty(this, P_PROTO, {
value: proto,
enumerable: false,
configurable: false,
writable: false
});
defineProperty(this, P_VALUE, {
value: {},
enumerable: false,
configurable: false,
writable: false
});
setProperties(this, proto);
}
},
enumerable: false,
configurable: false
});
defineProperty(F.prototype, O_PROTO, {
get: function fGetProto() {
if (typeof this.prototype === "function") {
return getPrototypeOf(this.constructor);
}
if (!(P_PROTO in this)) {
if (this.prototype) {
setProto(this, getPrototypeOf(this));
} else {
return O.prototype;
}
}
if (this[P_PROTO]) {
return typeof this[P_PROTO] === "function" ? this[P_PROTO] : this[P_PROTO].constructor;
} else {
return null;
}
},
set: function fSetProto(source) {
setProto(this, source);
},
enumerable: false,
configurable: false
});
}
})(Object, Function);