Created
July 1, 2016 17:13
-
-
Save xzure/eb98c7e89a98019720a69ad875045afa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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