-
Notifications
You must be signed in to change notification settings - Fork 549
/
hello.polyfill.js
152 lines (115 loc) · 2.77 KB
/
hello.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
// ES5 Object.create
if (!Object.create) {
// Shim, Object create
// A shim for Object.create(), it adds a prototype to a new object
Object.create = (function() {
function F() {}
return function(o) {
if (arguments.length != 1) {
throw new Error('Object.create implementation only accepts one parameter.');
}
F.prototype = o;
return new F();
};
})();
}
// ES5 Object.keys
if (!Object.keys) {
Object.keys = function(o, k, r) {
r = [];
for (k in o) {
if (r.hasOwnProperty.call(o, k))
r.push(k);
}
return r;
};
}
/* eslint-disable no-extend-native */
// ES5 [].indexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(s) {
for (var j = 0; j < this.length; j++) {
if (this[j] === s) {
return j;
}
}
return -1;
};
}
// ES5 [].forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun/*, thisArg*/) {
if (this === void 0 || this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t) {
fun.call(thisArg, t[i], i, t);
}
}
return this;
};
}
// ES5 [].filter
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun, thisArg) {
var a = [];
this.forEach(function(val, i, t) {
if (fun.call(thisArg || void 0, val, i, t)) {
a.push(val);
}
});
return a;
};
}
// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.io/#x15.4.4.19
if (!Array.prototype.map) {
Array.prototype.map = function(fun, thisArg) {
var a = [];
this.forEach(function(val, i, t) {
a.push(fun.call(thisArg || void 0, val, i, t));
});
return a;
};
}
// ES5 isArray
if (!Array.isArray) {
// Function Array.isArray
Array.isArray = function(o) {
return Object.prototype.toString.call(o) === '[object Array]';
};
}
// Test for location.assign
if (typeof window === 'object' && typeof window.location === 'object' && !window.location.assign) {
window.location.assign = function(url) {
window.location = url;
};
}
// Test for Function.bind
if (!Function.prototype.bind) {
// MDN
// Polyfill IE8, does not support native Function.bind
Function.prototype.bind = function(b) {
if (typeof this !== 'function') {
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
function C() {}
var a = [].slice;
var f = a.call(arguments, 1);
var _this = this;
var D = function() {
return _this.apply(this instanceof C ? this : b || window, f.concat(a.call(arguments)));
};
C.prototype = this.prototype;
D.prototype = new C();
return D;
};
}
/* eslint-enable no-extend-native */