合并数组
let arr1 = [1,3,5,7,9]
let arr2 = [2,4,6,8,10]
console.log(...arr1); //展开一个数组 1 3 5 7 9
let arr3 = [...arr1,...arr2]//连接数组[1,3,5,7,9,2,4,6,8,10]
解构赋值
let a = [1,2,3,4,5,6]
let [c,...d] = a
console.log(c); // 1
console.log(d); // [2,3,4,5,6]
//展开运算符必须放在最后一位
字符串转为数组,正确识别 32 位的 Unicode 字符
[...'siva'] // ['s','i','v','a']
[...'x\uD83D\uDE80y'].length // 3
函数中接收任意数量的参数
function sum(...numbers){
return numbers.reduce((preValue,currentValue)=>{
return preValue + currentValue
})
}
console.log(sum(1,2,3,4));
浅拷贝
用此方法只能浅拷贝第一层key、value
let person = {name:'tom',age:18}
let person2 = {...person}
//console.log(...person); //报错,展开运算符不能展开对象
person.name = 'jerry'
console.log(person2);//{name:'tom',age:18}
console.log(person);//{name:'jerry',age:18}
//合并
let person3 = {...person,name:'jack',address:"地球"}
console.log(person3);//{name:'jack',age:18,address:"地球"}