gpt4 book ai didi

javascript - ES2015/ES6 中的扩展语法与剩余参数

转载 作者:IT王子 更新时间:2023-10-29 02:58:00 27 4
gpt4 key购买 nike

我对 ES2015 中的扩展语法和剩余参数感到困惑。谁能用适当的例子解释它们之间的区别?

最佳答案

使用传播时,您将单个变量扩展为更多:

var abc = ['a', 'b', 'c'];
var def = ['d', 'e', 'f'];
var alpha = [ ...abc, ...def ];
console.log(alpha)// alpha == ['a', 'b', 'c', 'd', 'e', 'f'];

当使用剩余参数时,您将函数的所有剩余参数折叠到一个数组中:

function sum( first, ...others ) {
for ( var i = 0; i < others.length; i++ )
first += others[i];
return first;
}
console.log(sum(1,2,3,4))// sum(1, 2, 3, 4) == 10;

关于javascript - ES2015/ES6 中的扩展语法与剩余参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33898512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com