猿问

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

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而不是二?


守着一只汪
浏览 580回答 2
2回答

回首忆惘然

es6中rest 参数变量是一个数组,该变量将多余的参数放入这个数组中,所以最后一步的other值是:others= [[2,3]]而不是[2,3]所以alert(others.length)是1;

慕桂英3389331

function add(arr) {  arr.push(123)  console.log(arr.length)}let arr = [1]add(arr)console.log(add.length)这段代码会输出 2 1可见函数内对参数数组的操作并不会改变参数数组的length
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答