function deepClone(obj){//这个方法会有循环引用的问题,比如target.target=target
let tmp;
if(typeof obj !='object'){
return obj;
}else{
tmp = Array.isArray(obj) ? []:{}
}
for(let key in obj){
tmp[key] = deepClone(obj[key])
}
return tmp;
}
function deepClone(obj){
let tmp;
let wp = new WeakMap();
if(typeof obj !='object'){
return obj;
}else{
tmp = Array.isArray(obj) ? []:{}
}
if(wp.get(obj)){
return wp.get(obj);
}
wp.set(obj,tmp);
for(let key in obj){
tmp[key] = deepClone(obj[key])
}
return tmp;
}
let obj = {
a:1,
b:2,
c:{
a:[1,2,3],
b:function(){
console.log(this.a);
}
},
obj:''
}
//obj.obj =obj
let cpy = deepClone(obj);
//console.log(obj)
console.log(obj.c.b)
console