如果数组或对象中的元素只是简单类型,一层对象或者数组,就是深拷贝
如:
let staff1 = {name: 'xiaohong',age: 27};
let staff2 = {...staff};
staff2.name = 'xiaoming'
staff1 : {name: 'xiaohong',age: 27}
staff2:{name: "xiaoming", age: 27}
如果数组或对象中的元素是复杂类型,就是浅拷贝
let aa = {
age: 18,
name: 'aaa',
address: {
city: "hangzhou"
}
}
let bb = {...aa}
bb.address.city = 'shanghai'
aa: {
age: 18,
name: 'aaa',
address: {
city: 'shanghai'
}
}
bb: {
age: 18,
name: 'aaa',
address: {
city: 'shanghai'
}
}
扩展运算符深复制
let cc = { age: 18, name: 'aaa', address: { city: "hangzhou" } } let dd = { ...cc, address: { ...cc.address } } dd.address.city = 'shanghai' cc: { age: 18, name: 'aaa', address: { city: 'hangzhou' } } dd: { age: 18, name: 'aaa', address: { city: 'shanghai' } }