-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreduceRight.js
40 lines (36 loc) · 1.04 KB
/
reduceRight.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
/*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight
*/
// Production steps of ECMA-262, Edition 5, 15.4.4.22
// Reference: http://es5.github.io/#x15.4.4.22
if (!Array.prototype.reduceRight) {
Array.prototype.reduceRight = function(callback, initialValue) {
if (this === void 0 || this === null) {
throw new TypeError('Array.prototype.reduceRight called on null or undefined');
}
if (callback.__class__ !== 'Function') {
throw new TypeError(callback + ' is not a function');
}
var t = Object(this), len = t.length >>> 0, k = len - 1, value;
if (arguments.length > 1)
{
value = initialValue;
}
else
{
while (k >= 0 && !(k in t)) {
k--;
}
if (k < 0) {
throw new TypeError('Reduce of empty array with no initial value');
}
value = t[k--];
}
for (; k >= 0; k--) {
if (k in t) {
value = callback(value, t[k], k, t);
}
}
return value;
};
}