ES6-扩展运算符

扩展运算符(延展操作符):可以将数组或者对象或者字符串转化为以逗号为分隔符的参数序列

应用:

1.合并数组:

方法一

let arr1 = [1,2,3];
let arr2 = [4,5,6];
let arr3 = [...arr1,...arr2];
console.log(arr3);//[1,2,3,4,5,6]

方法二

let arr1 = [1,2,3];
let arr2 = [4,5,6];
arr1.push(...arr2);//相当于 arr1.push(4,5,6)
console.log(arr1);//[1,2,3,4,5,6]
2.将伪数组转换为真数组:
var divs = document.getElementsByTagName("div");
console.log(Array.isArray(divs));//伪数组 false
var arr = [...divs];
console.log(Array.isArray(arr));//true   
传多个参:
function f(name,...numbers) {
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
alert(name);
alert(sum);
}
f("张三",1,2,3,4,5,6,7,8,9);

你可能感兴趣的:(ES6-扩展运算符)