深拷贝

function isObject(obj) {
  const type = typeof obj
  return obj !== null && (type == "function" || type == "object")
}

function deepClone(orginObj,map = new WeekMap()) {
  if(typeof orginObj == "function") {
    return orginObj
  }
  if(!isObject(orginObj)) {
    return orginObj
  }
  if(map.has(orginObj)) {
    return orginObj
  }
  const targetObj = Array.isArray(orginObj)?[]:{}
  map.set(orginObj,targetObj)
  const keys = Object.keys(orginObj)
  for(let key of keys) {
    targetObj[key] = deepClone(orginObj[key],map)
  }
  return targetObj
}```
posted @ 2022-05-06 15:18  coderlq  阅读(18)  评论(0)    收藏  举报