//深拷贝
var arr1 = [1, 2, 3];
var arr2 = arr1.slice();
arr1.push(4);
console.log(arr2) //[1, 2, 3]
//浅拷贝
var arr1 = [{a:1}];
arr1[0].a = 2;
console.log(arr2[0].a) //2
同样的方法,一会是深拷贝一会是浅拷贝,对数组来说,深拷贝和浅拷贝得看数组的格式吗?
蝴蝶刀刀
相关分类