...运算符
扩展运算符
var arr = [1,2,3,4,5,6];
var arr1 = [4,5,6,...arr];
console.log(arr1);//4,5,6,1,2,3,4,5,6
rest运算符
ES6引入rest参数(形式为“...变量名”),用于获取函数的多余参数,这样就不需要使用arguments对象了。
**注: **rest运算符(例: ...arr)一定要放到最后;
//例1:
var [a, ...arr] = [1,2,3];
console.log(arr);//2, 3
//例2:
function func(...values) {
var sum = 0;
for (var value of values.valueOf()) {
sum = sum + value;
}
return sum;
}
console.log(func(1,2,3));//6
网友评论