答案
C
解析
[2,1,0].reduce(Math.pow)
等价于
[2,1,0].reduce((accumulator, currentValue) => Math.pow(accumulator, currentValue), 0) ==> 输出结果为:1
答案
C解析
arr.reduce(callback[, initialValue])
- reduce接受两个参数, 一个回调, 一个初始值
- 回调函数接受四个参数 previousValue, currentValue, currentIndex, array
- 需要注意的是 If the array is empty and no initialValue was provided, TypeError would be thrown.
所以第二个会报异常. 第一个表达式等价于 Math.pow(2, 1) => 2; Math.pow(2, 0) =>1
答案是A
[2,1,0].reduce(Math.pow)
等价于
[2,1,0].reduce((accumulator, currentValue) => {
console.log(accumulator, currentValue)
return Math.pow(accumulator, currentValue)
} , 0)
// 0, 2
// 0, 1
// 0, 0
Math(0, 2) ==> 0
Math(0, 1) ==> 0
Math(0, 0) ==> 1
输出结果为 1
[2,1,0].reduce(Math.pow) 等价于:
[2, 1, 0].reduce((accumulator, currentValue) => {
console.log(accumulator, currentValue, Math.pow(accumulator, currentValue))
return Math.pow(accumulator, currentValue)
}, 2)
Math.pow(2, 2) ==> 4
Math.pow(4, 1) ==> 4
Math.pow(4, 0) ==> 1
输出结果为 1