Skip to content

Instantly share code, notes, and snippets.

@izumskee
Forked from xzure/array.reduce.polyfill.js
Created December 6, 2017 09:03
Show Gist options
  • Save izumskee/90af72804729045890af1afd1066dd52 to your computer and use it in GitHub Desktop.
Save izumskee/90af72804729045890af1afd1066dd52 to your computer and use it in GitHub Desktop.
const reduce = function(callback) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.reduce called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var t = Object(this),
len = t.length >>> 0,
k = 0,
value;
if (arguments.length == 2) {
value = arguments[1];
} else {
while (k < len && !(k in t)) {
k++;
}
if (k >= len) {
throw new TypeError('Reduce of empty array with no initial value');
}
value = t[k++];
}
for (; k < len; k++) {
if (k in t) {
value = callback(value, t[k], k, t);
}
}
return value;
};
if (!Array.prototype.reduce) Array.prototype.reduce = reduce;
if (!Float32Array.prototype.reduce) Float32Array.prototype.reduce = reduce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment