擴(kuò)展運(yùn)算符(延展操作符):可以將數(shù)組或者對(duì)象或者字符串轉(zhuǎn)化為以逗號(hào)為分隔符的參數(shù)序列
應(yīng)用:
1.合并數(shù)組:
方法一
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);//相當(dāng)于 arr1.push(4,5,6)
console.log(arr1);//[1,2,3,4,5,6]
2.將偽數(shù)組轉(zhuǎn)換為真數(shù)組:
var divs = document.getElementsByTagName("div");
console.log(Array.isArray(divs));//偽數(shù)組 false
var arr = [...divs];
console.log(Array.isArray(arr));//true
傳多個(gè)參:
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);