SOURCE

//深拷贝

let obj = {
    name:'sunny',
    age: 30,
    arr: [1,2,3,4]
}
let obj1 = obj
// console.log(obj1)
// obj1.name = 'cherry'
// console.log(obj1)
let copyObj = { ...obj }
copyObj.name = 'cherry'
copyObj.arr[0] = 8
console.log(JSON.stringify(copyObj))

console.log(JSON.stringify(obj))





//深拷贝实例
console.log("深拷贝实例")
const deepClone = (val) => {
  let res;
  if (val && Array.isArray(val)) {
    res = [];
    val.forEach((item) => {
      res.push(deepClone(item));
    });
  } else if (typeof val === 'object' && val !== null) {
    res = {};
    for (let key of Object.keys(val)) {
      res[key] = deepClone(val[key]);
    }
  } else {
    res = val;
  }
  return res;
}
console 命令行工具 X clear

                    
>
console