对象复制

Object.prototype.maps = function(){
  let newObj = new Object();
  let that = this;
  Object.keys(that).forEach(function(k){
    if( that[k].constructor == Object){
      newObj[k] = that[k].maps()
    }else if(that[k].constructor == Array){
      newObj[k] = that[k].maps()
    }else{
      newObj[k] = that[k]
    }
  })  
  return newObj;
}

数组复制

Array.prototype.maps = function(){
  let newArr = [];
  for(v of that){
    if( v.constructor == Object ){
      newArr.push(v.maps())
    }else if( v.constructor == Array ){
      newArr.push(v.maps())
    }else{
      newArr.push(v)
    }
  }
  return newArr;
}

个人原创,如有意见欢迎指正

posted on 2018-08-06 15:31  YC小杨  阅读(272)  评论(0编辑  收藏  举报