let resource = {
a: 'a',
address: {
city: 'beijing'
},
arr: [1, 3 , 5]
}
console.log(resource.address.city)
let tmp = deepCopy(resource)
tmp.address.city = 'shanghai'
console.log(resource.address.city)
function deepCopy(obj={}) {
if (typeof obj !== 'object' || obj == null){
return obj
}
// 初始化
let result
if (obj instanceof Array) {
result = []
} else{
result = {}
}
for(let key in obj){
// 保证key 不是原型属性
if (obj.hasOwnProperty(key)){
// !important 递归调用
result[key] = deepCopy(result[key])
}
}
return result
}
console