Spread操作符vs Rest参数在ES2015 / es6中 [英] Spread operator vs Rest parameter in ES2015/es6
本文介绍了Spread操作符vs Rest参数在ES2015 / es6中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对ES2015中 rest参数
中的 spread operator
感到困惑。任何人都可以用正确的例子解释它们之间的区别?
I am confused about spread operator
in rest parameter
in ES2015. Can anybody explain the difference between them with proper example?
推荐答案
使用扩展时,您将单个变量扩展为更多: / p>
When using spread, you are expanding a single variable into more:
var abc = ['a', 'b', 'c'];
var def = ['d', 'e', 'f'];
var alpha = [ ...abc, ...def ];
// alpha == ['a', 'b', 'c', 'd', 'e', 'f'];
使用休息参数时,您将一个函数的所有剩余参数折叠成一个数组:
When using rest arguments, you are collapsing all remaining arguments of a function into one array:
function sum( first, ...others ) {
for ( var i = 0; i < others.length; i++ )
first += others[i];
return first;
}
// sum(1, 2, 3, 4) == 10;
这篇关于Spread操作符vs Rest参数在ES2015 / es6中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文