看代码:
let obj1 = { name: '张三', age: 18, sex: '男' } let obj2 = obj1 console.log('obj2:', obj2) obj2.age = 22 console.log('obj2:', obj2) console.log('obj1:', obj1)
输出:
解决:
采用深拷贝的方法 :
let obj2 = JSON.parse(JSON.stringify(obj1))
看代码:
let obj1 = { name: '张三', age: 18, sex: '男' } let obj2 = obj1 console.log('obj2:', obj2) obj2.age = 22 console.log('obj2:', obj2) console.log('obj1:', obj1)
输出:
解决:
采用深拷贝的方法 :
let obj2 = JSON.parse(JSON.stringify(obj1))
转载于:https://www.cnblogs.com/guangzhou11/p/11498166.html