Skip to content

Commit 6387caf

Browse files
author
Eric Wendelin
committed
Add polyfills for old IEs.
1 parent 5b9f9f8 commit 6387caf

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

polyfills.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// ES5 Polyfills
2+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
3+
if (!Function.prototype.bind) {
4+
Function.prototype.bind = function (oThis) {
5+
if (typeof this !== 'function') {
6+
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
7+
}
8+
9+
var aArgs = Array.prototype.slice.call(arguments, 1);
10+
var fToBind = this;
11+
var NoOp = function () {
12+
};
13+
var fBound = function () {
14+
return fToBind.apply(this instanceof NoOp && oThis ? this : oThis,
15+
aArgs.concat(Array.prototype.slice.call(arguments)));
16+
};
17+
18+
NoOp.prototype = this.prototype;
19+
fBound.prototype = new NoOp();
20+
21+
return fBound;
22+
};
23+
}
24+
25+
26+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
27+
if (!Array.prototype.map) {
28+
Array.prototype.map = function(callback, thisArg) {
29+
if (this === void 0 || this === null) {
30+
throw new TypeError('this is null or not defined');
31+
}
32+
var O = Object(this);
33+
var len = O.length >>> 0;
34+
var T;
35+
if (typeof callback !== 'function') {
36+
throw new TypeError(callback + ' is not a function');
37+
}
38+
if (arguments.length > 1) {
39+
T = thisArg;
40+
}
41+
42+
var A = new Array(len);
43+
var k = 0;
44+
45+
while (k < len) {
46+
var kValue, mappedValue;
47+
if (k in O) {
48+
kValue = O[k];
49+
mappedValue = callback.call(T, kValue, k, O);
50+
A[k] = mappedValue;
51+
}
52+
k++;
53+
}
54+
55+
return A;
56+
};
57+
}
58+
59+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
60+
if (!Array.prototype.filter) {
61+
Array.prototype.filter = function(callback/*, thisArg*/) {
62+
if (this === void 0 || this === null) {
63+
throw new TypeError('this is null or not defined');
64+
}
65+
66+
var t = Object(this);
67+
var len = t.length >>> 0;
68+
if (typeof callback !== 'function') {
69+
throw new TypeError(callback + ' is not a function');
70+
}
71+
72+
var res = [];
73+
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
74+
for (var i = 0; i < len; i++) {
75+
if (i in t) {
76+
var val = t[i];
77+
if (callback.call(thisArg, val, i, t)) {
78+
res.push(val);
79+
}
80+
}
81+
}
82+
83+
return res;
84+
};
85+
}
86+
87+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
88+
if (!Array.prototype.forEach) {
89+
Array.prototype.forEach = function(callback, thisArg) {
90+
var T, k;
91+
if (this === null || this === undefined) {
92+
throw new TypeError(' this is null or not defined');
93+
}
94+
95+
var O = Object(this);
96+
var len = O.length >>> 0;
97+
if (typeof callback !== 'function') {
98+
throw new TypeError(callback + ' is not a function');
99+
}
100+
101+
if (arguments.length > 1) {
102+
T = thisArg;
103+
}
104+
k = 0;
105+
while (k < len) {
106+
var kValue;
107+
if (k in O) {
108+
kValue = O[k];
109+
callback.call(T, kValue, k, O);
110+
}
111+
k++;
112+
}
113+
};
114+
}

0 commit comments

Comments
 (0)