1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| function deepClone(obj,hash=new WeakMap()) { if(obj===null) return null if(obj instanceof Date) return new Date(obj); if(obj instanceof RegExp) return new RegExp(obj); if(hash.has(obj)) return hash.get(obj);
let cloneObj={}; if(obj inatanceof Map){ cloneObj=new Set(); obj.forEach(value=>{ cloneObj.add(deeepClone(value,hash)); });
}else{ cloneObj=new obj.construct(); hsh.set(obj,cloneObj); for(let key in obj){ if(obj.hasOwnProperty(key)){ cloneObj[key]=deepClone(obj[key],hash); } } } return cloneObj;
}
|