浅拷贝:藕断丝连
深拷贝:老死不相往来
浅拷贝:arr.slice() arr.concat() 还有ES6定义的Object.assign() 第一个参数为目标参数,之后跟一个或多个源对象
赋值号对于基本类型是深拷贝,对于引用类型是浅拷贝
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19let a = 1
let b = a
b = 2
console.log(a,b);//1 2 深拷贝
let arr = [1,2,3]
let brr = arr
brr.push(4)
console.log(arr,brr); //[1,2,3,4] [1,2,3,4] 浅拷贝
let obj = {
name: "ni",
age: 18,
attr: {sex:'女',x:"student"}
}
let newObj = obj
newObj.age = 28
newObj.attr.x = 'teach'
console.log(obj,newObj); //都改变 浅拷贝