ES6之展开运算符(...)
https://www.jianshu.com/p/3935a80342a0
合并 对象/数组
let a = [1,2,3]; let b = [4,5,6]; let c = [...a,...b]; // [1,2,3,4,5,6]
浅拷贝对象/数组
用扩展运算符对数组或者对象进行拷贝时,只能扩展和深拷贝第一层的值,对于第二层极其以后的值,扩展运算符将不能对其进行打散扩展,也不能对其进行深拷贝,即拷贝后和拷贝前第二层中的对象或者数组仍然引用的是同一个地址,其中一方改变,另一方也跟着改变。
var obj = {name:'范顺',age:18} var obj2 = {...obj} obj.name = '小天' console.log(obj2) //{name: "范顺", age: 18} var data = [{name:'范顺',age:18}, {name: '小天', age: 19}] var data2 = [...data] data[0].age = 100 console.log(data2) //[{name:'范顺',age:100}, {name: '小天',age:19}]
数组解构赋值
var arr = [1, 2, 3, 4] var [a, ...b] = arr ////展开运算符必须放在最后一位 console.log(a) // 1 console.log(b) // [2 ,3 ,4]