/**
* @desc
*/
function deepClone(obj, cache = new WeakMap()) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj);
}
if (obj instanceof RegExp) {
return new RegExp(obj);
}
// 避免循环引用
if (cache.has(obj)) {
return cache.get(obj);
}
// 创建一个新的对象
const cloneObj = new obj.constructor() // 保持原型链
for (let key in cloneObj) {
if (obj.hasOwnProperty(key)) {
cloneObj[key] = deepClone(obj[key], cache);
}
}
return cloneObj;
}