const map = {
array: "Array",
object: "Object",
function: "Function",
number: "Number",
null: "Null",
undefined: "Undefined",
boolean: "Boolean",
};
function getType(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}
function isTypeOf(obj, type) {
return map[type] && getType(obj) === map[type];
}
function deepClone(obj, circleArr = []) {
let result = isTypeOf(obj) == "Array" ? [] : {};
if (isTypeOf(obj ,"array") || isTypeOf(obj,"object")) {
let index = circleArr.indexOf(obj);
if (index !== -1) {
result = obj[index];
} else {
cicleArr.push(obj)
for (item in obj) {
result[item] = deepClone(obj[item], circleArr);
}
}
} else if (isTypeOf(obj,"function") ) {
result = eval("(" + obj.toString() + ")");
} else {
result = obj;
}
return result;
}
let obj = {
a: 1,
b: () => console.log(1),
c: {
d: 3,
e: 4,
},
f: [1, 2],
und: undefined,
nul: null,
};
console.log(deepClone(obj)===obj);