1.数组拷贝
let arr1 =[1,2,3,4];
let arr2 = arr1;//[1,2,3,4]
console.log(arr2);
arr2.push(5)
console.log(arr1);//[1,2,3,4,5]
// arr2添加了一个元素,arr1也会一起改变,这是因为arr1和arr2指向的内
//存空间是一致的,只要一个修改,另一个也
// 会同时改变,如果不想要这样,可以使用扩展运算符来赋值
let arr1 =[1,2,3,4];
let arr2 = [...arr1];//这里其实是赋值给arr2的是arr1的值,而不是对象
console.log(arr2);//[1,2,3,4]
arr2.push(5);
console.log(arr1);//[1,2,3,4]
console.log(arr2);//[1,2,3,4,5]
2.对象拷贝
let aa = {
name:'111',
}
let bb = {...aa}
bb.age=18;
console.log(aa)//{name: "111"}
console.log(bb)//{name: "111", age: 18}
网友评论