rest運(yùn)算符也是三個(gè)點(diǎn)號(hào),不過其功能與擴(kuò)展運(yùn)算符恰好相反叭喜,把逗號(hào)隔開的值序列組合成一個(gè)數(shù)組社搅。
//主要用于不定參數(shù)憎茂,所以ES6開始可以不再使用arguments對(duì)象
var bar = function(...args) {
for (let el of args) {
console.log(el);
}
}
bar(1, 2, 3, 4);
//1
//2
//3
//4
bar = function(a, ...args) {
console.log(a);
console.log(args);
}
bar(1, 2, 3, 4);
//1
//[ 2, 3, 4 ]
rest運(yùn)算符配合解構(gòu)使用:
var [a, ...rest] = [1, 2, 3, 4];
console.log(a);//1
console.log(rest);//[2, 3, 4]