Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
if (!Array.prototype.forEach) {
2
3
Array.prototype.forEach = function(callback, thisArg) {
4
5
var T, k;
6
7
if (this == null) {
8
throw new TypeError(' this is null or not defined');
9
}
10
11
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
12
var O = Object(this);
13
14
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
15
// 3. Let len be ToUint32(lenValue).
16
var len = O.length >>> 0;
17
18
// 4. If IsCallable(callback) is false, throw a TypeError exception.
19
// See: http://es5.github.com/#x9.11
20
if (typeof callback !== "function") {
21
throw new TypeError(callback + ' is not a function');
22
}
23
24
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
25
if (arguments.length > 1) {
26
T = thisArg;
27
}
28
29
// 6. Let k be 0
30
k = 0;
31
32
// 7. Repeat, while k < len
33
while (k < len) {
34
35
var kValue;
36
37
// a. Let Pk be ToString(k).
38
// This is implicit for LHS operands of the in operator
39
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
40
// This step can be combined with c
41
// c. If kPresent is true, then
42
if (k in O) {
43
44
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
45
kValue = O[k];
46
47
// ii. Call the Call internal method of callback with T as the this value and
48
// argument list containing kValue, k, and O.
49
callback.call(T, kValue, k, O);
50
}
51
// d. Increase k by 1.
52
k++;
53
}
54
// 8. return undefined
55
};
56
}
57
58
if (!Array.isArray) {
59
Array.isArray = function(arg) {
60
return Object.prototype.toString.call(arg) === '[object Array]';
61
};
62
}
63
64
if (!Array.prototype.map) {
65
66
Array.prototype.map = function(callback, thisArg) {
67
68
var T, A, k;
69
70
if (this == null) {
71
throw new TypeError(' this is null or not defined');
72
}
73
74
// 1. Let O be the result of calling ToObject passing the |this|
75
// value as the argument.
76
var O = Object(this);
77
78
// 2. Let lenValue be the result of calling the Get internal
79
// method of O with the argument "length".
80
// 3. Let len be ToUint32(lenValue).
81
var len = O.length >>> 0;
82
83
// 4. If IsCallable(callback) is false, throw a TypeError exception.
84
// See: http://es5.github.com/#x9.11
85
if (typeof callback !== 'function') {
86
throw new TypeError(callback + ' is not a function');
87
}
88
89
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
90
if (arguments.length > 1) {
91
T = thisArg;
92
}
93
94
// 6. Let A be a new array created as if by the expression new Array(len)
95
// where Array is the standard built-in constructor with that name and
96
// len is the value of len.
97
A = new Array(len);
98
99
// 7. Let k be 0
100
k = 0;
101
102
// 8. Repeat, while k < len
103
while (k < len) {
104
105
var kValue, mappedValue;
106
107
// a. Let Pk be ToString(k).
108
// This is implicit for LHS operands of the in operator
109
// b. Let kPresent be the result of calling the HasProperty internal
110
// method of O with argument Pk.
111
// This step can be combined with c
112
// c. If kPresent is true, then
113
if (k in O) {
114
115
// i. Let kValue be the result of calling the Get internal
116
// method of O with argument Pk.
117
kValue = O[k];
118
119
// ii. Let mappedValue be the result of calling the Call internal
120
// method of callback with T as the this value and argument
121
// list containing kValue, k, and O.
122
mappedValue = callback.call(T, kValue, k, O);
123
124
// iii. Call the DefineOwnProperty internal method of A with arguments
125
// Pk, Property Descriptor
126
// { Value: mappedValue,
127
// Writable: true,
128
// Enumerable: true,
129
// Configurable: true },
130
// and false.
131
132
// In browsers that support Object.defineProperty, use the following:
133
// Object.defineProperty(A, k, {
134
// value: mappedValue,
135
// writable: true,
136
// enumerable: true,
137
// configurable: true
138
// });
139
140
// For best browser support, use the following:
141
A[k] = mappedValue;
142
}
143
// d. Increase k by 1.
144
k++;
145
}
146
147
// 9. return A
148
return A;
149
};
150
}
151
152