js 递归深度拷贝方法封装

export function deepClone (obj) {
  if (obj === null || typeof obj !== 'object') {
    return obj
  }
  let clone
  if (Array.isArray(obj)) {
    clone = []
    for (let i = 0; i < obj.length; i++) {
      clone[i] = deepClone(obj[i])
    }
  } else {
    clone = {}
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        clone[key] = deepClone(obj[key])
      }
    }
  }
  return clone
}

你可能感兴趣的:(javascript,前端)