Skip to content

Commit db40eef

Browse files
authored
Update index.js
1 parent a27cccb commit db40eef

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,43 @@ if (!Array.from) {
7878
}());
7979
}
8080

81+
//reduce.js
82+
/*
83+
Source of the polyfill:
84+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Polyfill
85+
86+
*/
87+
88+
if(!Array.prototype.reduce){
89+
90+
Array.prototype.reduce = function(callback) {
91+
'use strict';
92+
if (this == null) {
93+
throw new TypeError('Array.prototype.reduce called on null or undefined');
94+
}
95+
if (typeof callback !== 'function') {
96+
throw new TypeError(callback + ' is not a function');
97+
}
98+
var t = Object(this), len = t.length >>> 0, k = 0, value;
99+
if (arguments.length == 2) {
100+
value = arguments[1];
101+
} else {
102+
while (k < len && !(k in t)) {
103+
k++;
104+
}
105+
if (k >= len) {
106+
throw new TypeError('Reduce of empty array with no initial value');
107+
}
108+
value = t[k++];
109+
}
110+
for (; k < len; k++) {
111+
if (k in t) {
112+
value = callback(value, t[k], k, t);
113+
}
114+
}
115+
return value;
116+
};
117+
}
81118
//assign.js
82119
/**
83120
* https://gist.github.com/WebReflection/10404826

0 commit comments

Comments
 (0)