手写实现深拷贝

81 阅读1分钟
function deepClone(obj ={}) {
  if (typeof obj !== 'object' || obj == {}) {
    return obj
  }
  let result
  if (obj instanceof Array) {
    result = []
  } else {
    result = {}
  }
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      result[key] = deepClone(obj[key])
    }
  }
  return result
}