/**
  * 深拷贝
  * @param {Object} obj 要拷贝的对象
  * obj.hasOwnProperty(key)报错,更改为Object.prototype.hasOwnProperty(obj,key)
  */
  function deepClone(obj = {}) {
    if (typeof obj !== 'object' || obj == null) {
        // obj 是 null ,或者不是对象和数组,直接返回
        return obj
    }
 
    // 初始化返回结果
    let result
    if (obj instanceof Array) {
        result = []
    } else {
        result = {}
    }

    for (let key in obj) {
    // 保证 key 不是原型的属性
        if (Object.prototype.hasOwnProperty(obj,key)) {
        // 递归调用!!!
            result[key] = deepClone(obj[key])
        }
    }

     // 返回结果
    return result
  }
/**
  * 浅拷贝
 */
  var peopleObj={
    name:'ming',
    age:12
  }
  var copyPeople = {}
  Object.assign(copyPeople,peopleObj)

用户bPbA4lM
103 声望9 粉丝