Skip to content

Latest commit

 

History

History
36 lines (29 loc) · 462 Bytes

File metadata and controls

36 lines (29 loc) · 462 Bytes

实现深拷贝

代码

function deepCopy(o) {
  if (typeof o !== "object") return o;

  if (o instanceof Array) {
    o.forEach(v => (o[v] = deepCopy(v)));
  }

  // reg math function 等其他类型暂时不考虑
  if (o instanceof Object) {
    Object.keys(key => {
      o[key] = deepCopy(o[key]);
    });
  }

  return o;
}

const a = deepCopy({
  a: [
    1,
    [4],
    {
      a: {
        c: [4]
      }
    }
  ]
});

console.log(a);