为了账号安全,请及时绑定邮箱和手机立即绑定

ES6的三点运算符号为什么会出现这种情况?

ES6的三点运算符号为什么会出现这种情况?

守着一只汪 2019-02-07 18:26:41
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;


查看完整回答
反对 回复 2019-02-19
?
慕桂英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


查看完整回答
反对 回复 2019-02-19
  • 2 回答
  • 0 关注
  • 579 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号