Array.prototype.reduce()
目录
函数参数:
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
源码:
function ArrayReduce(callback, current) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce");
// Pull out the length so that modifications to the length in the
// loop will not affect the looping and side effects are visible.
var array = TO_OBJECT(this);
var length = TO_LENGTH(array.length);
return InnerArrayReduce(callback, current, array, length,
arguments.length);
}
function InnerArrayReduce(callback, current, array, length, argumentsLength) {
// 判断传入的函数是否能被调用
if (!IS_CALLABLE(callback)) {
throw %make_type_error(kCalledNonCallable, callback);
}
var i = 0;
// 参数只有callback时,current作第一个参数
find_initial: if (argumentsLength < 2) {
for (; i < length; i++) {
if (i in array) {