深拷贝

130 阅读1分钟
/**
* 深拷贝
* Params {Object}
* return Object
*/
function deepClone(obj) {
  if(typeof obj !== 'object' || obj == null) {
      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
}