const obj = {
a: 1,
b: {
c: 2,
d: '111',
e: new Date(),
f: function() {
return '1';
}
}
}
const t = {m: obj};
obj.k = t;
// function app() {
// return ap() {
// }
// }
function deepClone(obj, map=new Map()) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (map.has(obj)) {
return map.get(obj);
}
if (obj instanceof Function) {
const res = new Function("return " + obj.toString())();
return res;
}
if (obj instanceof Date) {
const res = new Date();
res.setTime(obj.getTime());
return res;
}
if (obj instanceof RegExp) {
const res = new RegExp(obj);
return res;
}
if (obj instanceof Array || obj instanceof Object) {
const cloneObj = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
map.set(obj, cloneObj);
}
cloneObj[key] = deepClone(obj[key], map);
}
}
return cloneObj;
}
}
const newObj = deepClone(obj);
console.log(deepClone(obj));
console