function clone(target, map = new WeakMap()) {
if (typeof target === 'object') {
let cloneTarget = Array.isArray(target) ? [] : {};
if (map.get(target)) {
return map.get(target);
}
map.set(target, cloneTarget);
for (const key in target) {
cloneTarget[key] = clone(target[key], map);
}
return cloneTarget;
} else {
return target;
}
}
const target = {
field1: 1,
field2: undefined,
field3: {
child: 'child'
},
field4: [2, 4, 8],
fn: () => {
console.log(123);
}
};
target.target = target;
const test = clone(target);
console.log(test.target);
05-04
1366
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-25