arr.reduce(callback[, initialValue])
callback
- 执行数组中每个值的函数,包含四个参数:
accumulator- 累计器累计回调的返回值;它是上一次调用回调时返回的累积值,或
initialValue
currentValuecurrentIndex- 数组中正在处理的当前元素的索引。如果提供了
initialValue,则索引号为 0,否则索引为 1
array
initialValue
- 作为第一次调用
callback 函数时的第一个参数的值。如果没有提供初始值,则将使用数组中的第一个元素。在没有初始值的空数组上调用 reduce 将报错
- 判断
this 是否为 null 或 undefined - 判断
callback 是否为函数 - 初始化变量
- 如果没有提供
initialValue,则数组的第一个有效值作为累加器的初始值 k++ - 用上文
k 值继续进行遍历
* 数组的reduce方法
* @param {function} callback 回调函数
* @param {any} initialValue 初始值
* @returns {any} 返回值
*
* */
Array.prototype.reduce = function (callback, initialValue) {
if (this === null || this === undefined) {
throw new TypeError('Cannot read property "reduce" of null or undefined');
}
if (Object.prototype.toString.call(callback) !== '[object Function]') {
throw new TypeError(callback + ' is not a function');
}
* Object
* 当调用或者构造 Object() 构造函数本身时,其返回值是一个对象。
* 如果该值是 null 或者 undefined,它会生成并返回一个空对象。
* 如果该值已经是一个对象,则返回该值。
* 否则,它将返回与给定值对应的类型的对象。
* */
let O = Object(this);
* 所有非数值转换成0;
* 所有大于等于 0 等数取整数部分;
* */
let len = O.length >>> 0;
let k = 0;
let accumulator = initialValue;
if (accumulator === undefined) {
if (len === 0) {
throw new TypeError('Reduce of empty array with no initial value');
}
for (; k < len; k++) {
if (k in O) {
accumulator = O[k];
k++;
break;
}
}
}
for (; k < len; k++) {
if (k in O) {
accumulator = callback.call(undefined, accumulator, O[k], k, O);
}
}
return accumulator;
}