A 140 bytes Array.prototype.reduce solution.
It should work as described on MDN.
If the array has no value and no initial value is passed, it raises an error. If the callback is not a function, it will fail when tempting to call it.
There is one major difference between ES5 specifications and this implementation: if the second parametter is specified but undefined
, it won't be taken as initial value. There is no place to put a arguments.length
here :(
This fallback is not ES5 compliant.
Also browsers like Chrome have a bug where
Array.prototype.reduce = [].reduce
will causeArray#reduce
to become enumerable.For more information on what can be changed to make this method ES5 complaint and info on a critical bug, that this gist shares, please see my comments on a similar gist.