let a ={}
let b ={}
a.b = b
b.a = a
function deepClone(obj, hash = new WeakMap()) {
console.log(hash)
if (hash.has(obj)) {
// 如果已经拷贝过该对象,直接返回
return hash.get(obj);
}
if (typeof obj === 'object') {
// 如果不是对象或者为null,直接返回
var result = obj instanceof Array ? [] : {};
hash.set(obj, result);
}
// 递归拷贝对象属性
for(let i in obj){
result[i]=deepClone(obj[i],hash)
}
return result;
}
let result=deepClone(a)
console.log(result)
Using WeakMap in the deepClone function can work correctly,but if think about the logic behind it, the function may encounter an endless loop