JS 手写深拷贝
function deepClone(obj,hash = new WeakMap()) {
if (obj == null) return obj;
if (obj instanceof Date) return new Date(obj);
if (obj instanceof RegExp) return new RegExp(obj);
if (typeof obj !== "object") return obj;
if (hash.has(obj)) return hash.get(obj);
let cloneObj = new obj.constructor();
hash.set(obj,cloneObj);
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
cloneObj[key] = deepClone(obj[key],hash);
}
}
return cloneObj;
}
const obj = { age: { name: "小林" } };
const n = deepClone(obj);
n.age.name = "小n";
console.log(obj);
console.log(n);