-
Notifications
You must be signed in to change notification settings - Fork 320
/
polyfill.js
executable file
·305 lines (280 loc) · 9.38 KB
/
polyfill.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
(function (global) {
var oProto = Object.prototype;
var toString = oProto.toString;
var hasOwnProperty = oProto.hasOwnProperty;
function needFix(fn) {
return !/native code/.test(fn);
}
//修复 console
if (!global.console) {
global.console = {};
}
var con = global.console; //有的浏览器拥有console对象,但没有这么多方法
var prop, method;
var dummy = function () { };
var properties = ["memory"];
var methods = ("assert,clear,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEn" +
"d,info,log,markTimeline,profile,profiles,profileEnd,show,table,time,timeEnd,time" +
"line,timelineEnd,timeStamp,trace,warn").split(",");
while ((prop = properties.pop())) {
if (!con[prop]) {
con[prop] = {};
}
}
while ((method = methods.pop())) {
if (!con[method]) {
con[method] = dummy;
}
}
// 修复console完毕!
//https://github.com/flowersinthesand/stringifyJSON/blob/master/stringifyjson.js
function quote(string) {
return (
"\"" +
string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === "string"
? c
: "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
}) +
"\""
);
}
function f(n) {
return n < 10 ? "0" + n : n;
}
function str(key, holder) {
var i,
v,
len,
partial,
value = holder[key],
type = typeof value;
if (
value &&
typeof value === "object" &&
typeof value.toJSON === "function"
) {
value = value.toJSON(key);
type = typeof value;
}
switch (type) {
case "string":
return quote(value);
case "number":
return isFinite(value) ? String(value) : "null";
case "boolean":
return String(value);
case "object":
if (!value) {
return "null";
}
switch (toString.call(value)) {
case "[object Date]":
return isFinite(value.valueOf())
? "\"" +
value.getUTCFullYear() +
"-" +
f(value.getUTCMonth() + 1) +
"-" +
f(value.getUTCDate()) +
"T" +
f(value.getUTCHours()) +
":" +
f(value.getUTCMinutes()) +
":" +
f(value.getUTCSeconds()) +
"Z\""
: "null";
case "[object Array]":
len = value.length;
partial = [];
for (i = 0; i < len; i++) {
partial.push(str(i, value) || "null");
}
return "[" + partial.join(",") + "]";
default:
partial = [];
for (i in value) {
if (hasOwnProperty.call(value, i)) {
v = str(i, value);
if (v) {
partial.push(quote(i) + ":" + v);
}
}
}
return "{" + partial.join(",") + "}";
}
}
}
if (typeof JSON === "undefined") {
var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
meta = {
"\b": "\\b",
"\t": "\\t",
"\n": "\\n",
"\f": "\\f",
"\r": "\\r",
"\"": "\\\"",
"\\": "\\\\"
};
global.JSON = {
stringify: function (value) {
return str("", { "": value });
},
//http://www.cnblogs.com/fengzekun/p/3940918.html
parse: function () {
return new Function("return " + data)();
}
};
}
//支持 Function.prototype.bind
var fProto = Function.prototype;
if (needFix(fProto.bind)) {
fProto.bind = function () {
var fn = this;
var presetArgs = [].slice.call(arguments);
var context = presetArgs.shift();
var curry = function () {
for (var i = 0, n = arguments.length; i < n; i++) {
presetArgs.push(arguments[i]);
}
return fn.apply(context, presetArgs);
};
curry.name = "bound " + (fn.name || "anonymous");
return curry;
};
}
//修正 Array.prototype.splice
var arrayProto = Array.prototype;
if (0 === [1, 2].splice(0).length) {
var _splice = arrayProto.splice;
arrayProto.splice = function (a) {
var args = arrayProto.slice.call(arguments);
if (typeof args[1] !== "number") {
//IE6-8只能重写已经存在的索引值。比如aaa(1,2,3),只有三个参数,不能以arguments[3] =88来添加第4个参数
args[1] = this.length;
}
return _splice.apply(this, args);
};
}
//支持 Array.prototype.forEach
if (needFix(arrayProto.forEach)) {
arrayProto.forEach = function (callback, thisArg) {
var array = this;
for (var i = 0, n = array.length; i < n; i++) {
if (i in array) {
callback.call(thisArg, array[i], i, array);
}
}
};
}
if (needFix(arrayProto.indexOf)) {
arrayProto.indexOf = function (searchElement, fromIndex) {
if ( this === undefined || this === null ) {
throw new TypeError( '"this" is null or not defined' );
}
var length = this.length >>> 0; // Hack to convert object.length to a UInt32
fromIndex = +fromIndex || 0;
if (Math.abs(fromIndex) === Infinity) {
fromIndex = 0;
}
if (fromIndex < 0) {
fromIndex += length;
if (fromIndex < 0) {
fromIndex = 0;
}
}
for (; fromIndex < length; fromIndex++) {
if (this[fromIndex] === searchElement) {
return fromIndex;
}
}
return -1;
};
}
if(!String.prototype.trim){
String.prototype.trim = function(a){
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
}
}
//支持 Array.isArray
if (needFix(Array.isArray)) {
Array.isArray = function (arr) {
return toString.call(arr) == "[object Array]";
};
}
//支持 Object.is
if (needFix(Object.is)) {
Object.is = function is(x, y) {
if (x === y) {
// Steps 1-5, 7-10 Steps 6.b-6.e: +0 != -0 Added the nonzero y check to make
// Flow happy, but it is redundant
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
};
}
if (needFix(Object.create)) {
Object.create = function (o) {
function F() { }
F.prototype = o;
return new F();
};
}
//支持 Object.assign
if (needFix(Object.assign)) {
Object.assign = function (target) {
if (target === undefined || target === null) {
throw new TypeError("Cannot convert undefined or null to object");
}
var output = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source !== undefined && source !== null) {
for (var nextKey in source) {
if (hasOwnProperty.call(source,nextKey)) {
output[nextKey] = source[nextKey];
}
}
}
}
return output;
};
}
if (needFix(Object.keys)) {
var hasDontEnumBug = !{ toString: null }.propertyIsEnumerable("toString");
var dontEnums = [
"toString",
"toLocaleString",
"valueOf",
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable",
"constructor"
];
Object.keys = function (obj) {
if (Object(obj) !== obj) {
throw new TypeError("Object.keys called on non-object");
}
var result = [],
prop,
i;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i = 0; i < 7; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}
})(typeof window === "undefined" ? this : window);