Array.prototype.reduce = Array.prototype.reduce || function(callfun) {
var arr = this;
var len = arr.length;
if (typeof callfun !== 'function') {
throw new TypeError('type error');
}
var accumulate = null;
var i = 0;
var kPresent = false;
var initialVal = arguments.length > 1 ? arguments[1] : null;
if (len === 0 && arguments.length < 2) {
throw new TypeError('Reduce of empty array with no initial value');
}
if (arguments.length > 1) {
accumulate = initialVal;
} else {
accumulate = arr[i];
++1;
}
while(k < len) {
// 判断是否为 empty [,,,]
kPresent = arr.hasOwnProperty(k);
if (kPresent) {
var val = arr[k];
accumulate = callfun.apply(undefined, [accumulate, val, k, arr]);
}
k++;
}
return accumulate;
}
console