function add(...others) { alert(others); alert(others.length); if(others.length === 1){ return function (another) { others.push(another); return add(others); } }else if (others.length > 1){ alert("aaa"); alert(others[0] + others[1]); return others[0] + others[1]; }else{ alert("dd"); return undefined; }}add(2)(3);//没有报错,alert依次输出 2 1 2,3 1,为什么最后是1而不是二?
2 回答

回首忆惘然
TA贡献1847条经验 获得超11个赞
es6中rest 参数变量是一个数组,该变量将多余的参数放入这个数组中,所以最后一步的other值是:
others= [[2,3]]
而不是
[2,3]
所以alert(others.length)是1;

慕桂英3389331
TA贡献2036条经验 获得超8个赞
function add(arr) {
arr.push(123)
console.log(arr.length)
}
let arr = [1]
add(arr)
console.log(add.length)
这段代码会输出 2 1
可见函数内对参数数组的操作并不会改变参数数组的length
添加回答
举报
0/150
提交
取消