自己使用的深拷贝方法

 
// 深拷贝
deepCopy (obj) {
  if(obj instanceof Date){
    // 注意 JSON.stringify   转Date类型时   会少8小时
    return new Date(new Date(obj).valueOf());
  }
  var result = Array.isArray(obj) ? [] : {};
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (typeof obj[key] === 'object' && obj[key]!==null) {
        result[key] = this.deepCopy(obj[key]); //递归复制
      } else {
        result[key] = obj[key];
      }
    }
  }
  return result;
}

猜你喜欢

转载自www.cnblogs.com/Mr-Rshare/p/12579750.html