浅拷贝
直接赋值,改变一个另一个跟着改变
const obj = { name:'李', age:16, gender:'男' } const obj2 = obj obj2.name = '王' console.log(obj.name) console.log(obj2.name)
深拷贝
改变其中一个对象,另一个不会变
1.JSON.parse(JSON.stringify(obj))
const obj = { name:'李', age:16, gender:'男' } const obj2 = JSON.parse(JSON.stringify(obj)) obj2.name = '王' obj2.age = 28 console.log(obj) //{ name: '李', age: 16, gender: '男' } console.log(obj2) //{ name: '王', age: 28, gender: '男' }
2.遍历