js深拷贝

function deepChone(obj = {}) {
  if (typeof obj !== 'object' || obj == null) {
    return obj
  }
  // 初始化返回结果
  let result;
  if (obj instanceof Array) {
    result = []
  }else{
    result = {}
  }
  for (let key in obj) {
    // 保证 key 不是原型的属性
    if (obj.hasOwnProperty(key)){
      // 递归
      result[key] = deepChone(obj[key])
    }
  }
  return result
}
Last modification:October 27th, 2020 at 11:25 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment