let shallow_copy_obj = function(source_obj){
let new_obj = {}
for(let key in source_obj){
new_obj[key] = source_obj[key]
}
return new_obj
}
let obj = {
a:1,
b:2,
c:{
d:3
},
e:[1,2,3]
}
let arr = [1,2,3,{a:1,b:2},[1,3]]
let deep_copy_obj = function(source){
let target = source instanceof Array?[]:{}
if(typeof source != 'object') return
for(let key in source)
target[key] = typeof source[key] === 'object'? deep_copy_obj(source[key]):source[key]
return target
}
function deepClone(obj = {}, map = new Map()) {
if (typeof obj !== "object") return obj;
if (map.get(obj)) return map.get(obj);
let result = obj instanceof Array?[]:{}
map.set(obj, result);
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = deepClone(obj[key], map);
}
}
return result;
}
let new_obj = deepClone(obj)
let new_arr = deepClone(arr)
obj.c.d = 2
obj.e[0] = -1
arr[3].a = 2
arr[4][0] = 2
console.log(obj)
console.log(new_obj)
console.log(arr)
console.log(new_arr)