1. 合并连接数组
let a = [1,2,3]; let b = [0, ...a, 4]; // [0,1,2,3,4]
2.解构赋值
let a = [1,2,3,4,5,6] let [c,...d] = a console.log(c); // 1 console.log(d); // [2,3,4,5,6] //展开运算符必须放在最后一位
// 也可以 let a = [1,2,3]; let [b, ...[c,d,e]] = a; b; // 1 c; // 2 d; // 3 e; // undefined // 也可以 function test(a, ...rest){ console.log(a); // 1 console.log(rest); // [2,3] } test(1,2,3)
3 深拷贝,浅拷贝的问题
(1)当只有一层的数组 或者对象 ,其元素就是简单类型,这时候就是深拷贝
let aa = { age: 18, name: 'name' } let bb = {...aa}; bb.age = 22; console.log(aa.age);//18
(2) 引用类型元素的时候 (2层或2层以上),就是浅拷贝
let aa = { age:18, name:'aaa', address: { city: 'shanghai' } } let bb={...aa}; bb.address.city = 'shenzhen'; console.log(aa.address.city); //shenzhen