init() {
const obj1 = {
age: 20,
name: 'xxx',
address: {
city: 'chongqing'
},
arr: ['a', 'b', 'c']
}
let obj2 = deepClone(obj1)
obj2.address.city = 'beijing'
console.log(`${obj1.address.city}`);
function deepClone(obj = {}) {
if (typeof obj !== 'object' || typeof obj == null) {
return obj
}
let result
if (obj instanceof Array) {
result = []
} else {
result = {}
}
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = deepClone(obj[key])
}
}
return result
}
}
js实现递归深度拷贝引用类型
最新推荐文章于 2024-10-02 05:39:15 发布