SOURCE

// function deepCopy(data) {
//       if(typeof data !== 'object' || data === null){
//             throw new TypeError('传入参数不是对象')
//         }
//       let newData = {};
//       const dataKeys = Object.keys(data);
//       dataKeys.forEach(value => {
//          const currentDataValue = data[value];
//          // 基本数据类型的值和函数直接赋值拷贝 
//          if (typeof currentDataValue !== "object" || currentDataValue === null) {
//               newData[value] = currentDataValue;
//           } else if (Array.isArray(currentDataValue)) {
//              // 实现数组的深拷贝
//             newData[value] = [...currentDataValue];
//           } else if (currentDataValue instanceof Set) {
//              // 实现set数据的深拷贝
//              newData[value] = new Set([...currentDataValue]);
//           } else if (currentDataValue instanceof Map) {
//              // 实现map数据的深拷贝
//              newData[value] = new Map([...currentDataValue]);
//           } else { 
//              // 普通对象则递归赋值
//              newData[value] = deepCopy(currentDataValue);
//           } 
//        }); 
//       return newData;
//   }


//  function deepclone (obj) {
//     if (typeof obj !== 'object' || obj == null) {
//         return obj;
//     }

//     if (obj instanceof Array) {
//         let res = [];
//         obj.forEach((item,index)=>{
//             res[index]=deepclone(item)
//         })
//         return res;
//     }

//     if (obj instanceof Object) {
//         let res = {};
//         for (const key in obj) {
            
//             if (obj.hasOwnProperty(key)) {
//                 res[key] = deepclone(obj[key]);
//             }
//         }
//         return res;
//     }
// }

let z = {
    a: 1,
    b: { f: { g: 1 } },
    c: [1, 2, 3]
}
let x= deepclone(z)
z.b.f.g=5
console.log(z===x)
console.log(x.b.f.g)

console 命令行工具 X clear

                    
>
console