深拷贝

let a = {
            a:1,
            b:2,
            c:{a:1,b:2}
        }
        
        function deepCopy(obj) {
          var result = Array.isArray(obj) ? [] : {};
          for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
              if (typeof obj[key] === 'object') {
                result[key] = deepCopy(obj[key]);   //递归复制
              } else {
                result[key] = obj[key];
              }
            }
          }
          return result;
        }

        
        console.log(deepClone(a))

你可能感兴趣的:(深拷贝)