浅克隆以及深度克隆的实现

浅克隆的实现:

function extendCopy(obj) {
  var newObj = {}
  for(var i in obj){
    newObj[i] = obj[i]
  }
  return newObj
}

深度克隆的实现:

function extendDeepCopy(obj, newObj){
  var newObj = newObj || {}
  for(var i in obj){
    if(typeof obj[i] == "object"){
      newObj[i] = (obj[i].constructor === Array) ? [] : {}
      extendDeepCopy(obj[i], newObj[i])
    }else{
      newObj[i] = obj[i]
    }
  }
  return newObj
}

参考:https://www.jianshu.com/p/afc55e33a36a

你可能感兴趣的:(浅克隆以及深度克隆的实现)