// 深拷贝
function cloneDeep(obj, map = 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 (map.get(obj)) return map.get(obj);
const cloneObj = new obj.constructor();
map.set(obj, cloneObj)
for (key in obj) {
if (obj.hasOwnProperty(key)) {
cloneObj[key] = cloneDeep(obj[key], map);
}
}
return cloneObj;
}
const obj = {
name: 'cc',
arr: [1, 2, 3],
getName() {
console.log(this.name);
}
}
const cloneObj = cloneDeep(obj)
cloneObj.arr.push(4)
cloneDeep.name = 'zz'
console.log(obj)
console.log(cloneObj)
obj.getName();
cloneObj.getName()
console.log(obj.getName === cloneObj.getName)
console