let map = new WeakMap()
function deepClone(obj){
if(typeof obj !== "object" || obj === null) {
return obj
}
let newObj
if(obj instanceof Array){
newObj = []
}else{
newObj = {}
}
if(map.has(obj)){
newObj = map.get(obj)
}else{
map.set(obj,newObj)
for(let key in obj){
if(obj.hasOwnProperty(key))//防止拷贝原型上的属性
newObj[key] = deepClone(obj[key])
}
}
return newObj
}
let obj2
let obj1 = {
name:"张三",
age:18,
backpage:[{name:1},1,2,3,{obj2}]
}
obj2 = {
obj:obj1
}
let newObj = deepClone(obj1)
console.log(obj1)
console.log(newObj)
console