在ES6中是否可以有超过rest参数作为函数参数?

is it possible to have more than rest parameter as function arguments in ES6?

本文关键字:参数 rest 函数 ES6 是否      更新时间:2023-09-26

我们可以有两个或更多的休息参数在函数参数使用ES6?有人能解释一下吗?

function f(a, b, ...args1, c, ...arg2) {
    //do somthing
}

这是可能的与es6?

No.

从MDN的其余参数:

如果函数的最后一个命名参数...为前缀,则该函数成为一个数组,其从0(含)到theArgs.length(不含)的元素由传递给函数的实际参数提供。

(强调我的)

如果是"the last",显然只能是一个

不能,因为结果不能很好地定义。

例如,如果你调用

f(1,2,3,4,5);

则会有不同的合理可能性:

a = 1;
b = 2;
args1 = [];
c = 3;
args2 = [4,5];
a = 1;
b = 2;
args1 = [3];
c = 4;
args2 = [5];
a = 1;
b = 2;
args1 = [3,4];
c = 5;
args2 = [];
a = 1;
b = 2;
args1 = [3,4,5];
c = undefined;
args2 = [];