亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解JS深拷貝與淺拷貝

 更新時間:2020年08月04日 11:21:36   作者:不語微菁年,莫思菪櫻  
這篇文章主要介紹了JS深拷貝與淺拷貝的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

一、預(yù)備知識

1.1、JS數(shù)據(jù)類型

基本數(shù)據(jù)類型:Boolean、String、Number、null、undefined
引用數(shù)據(jù)類型:Object、Array、Function、RegExp、Date等

1.2、數(shù)據(jù)類型的復(fù)制

基本數(shù)據(jù)類型的復(fù)制,是按值傳遞的

var a = 1;
var b = a;
b = 2;
console.log(a); // 1
console.lob(b); // 2

引用數(shù)據(jù)類型的復(fù)制,是按引用傳值

var obj1 = {
 a: 1;
 b: 2;
};
var obj2 = obj1;
obj2.a=3;
console.log(obj1.a); //3
console.log(obj2.a); // 3

1.3、深拷貝與淺拷貝

深拷貝和淺拷貝都只針對引用數(shù)據(jù)類型,淺拷貝會對對象逐個成員依次拷貝,但只復(fù)制內(nèi)存地址,而不復(fù)制對象本身,新舊對象成員還是共享同一內(nèi)存;深拷貝會另外創(chuàng)建一個一模一樣的對象,新對象跟原對象不共享內(nèi)存,修改新對象不會改到原對象。

區(qū)別:淺拷貝只復(fù)制對象的第一層屬性,而深拷貝會對對象的屬性進(jìn)行遞歸復(fù)制。

二、JS淺拷貝

2.1、賦值與淺拷貝

當(dāng)把一個對象賦值給一個新的變量時,賦的對象是該對象在棧中的地址,而不是堆中的數(shù)據(jù)。也就是新舊兩個對象指的是同一個存儲空間,無論哪個對象發(fā)生改變,其實(shí)都是改變的存儲空間的內(nèi)容,兩個對象聯(lián)動的會一起改變。

var obj1 = {
  'name' : 'zhangsan',
  'language' : [1,[2,3],[4,5]],
};
var obj2 = obj1;
obj2.name = "lisi";
obj2.language[1] = ["二","三"];
console.log('obj1',obj1)
console.log('obj2',obj2)

淺拷貝是按位拷貝對象,它會創(chuàng)建一個新對象,對原有對象的成員進(jìn)行依次拷貝。如果屬性是基本類型,拷貝的就是基本類型的值;如果屬性是引用類型,拷貝的就是內(nèi)存地址。因此如果新對象中的某個對象成員改變了地址,就會影響到原有的對象。

//手寫淺拷貝
function shallowCopy(obj1) {
 let obj2 = Array.isArray(obj1) ? [] : {}
 for (let i in obj1) {
  obj2[i] = obj1[i]
 }
 return obj2
}
var obj1 = {
  'name' : 'zhangsan',
  'language' : [1,[2,3],[4,5]],
};
var obj2 = shallowCopy(obj1);
obj2.name = "lisi";
obj2.language[1] = ["二","三"];
console.log('obj1',obj1)
console.log('obj2',obj2)

2.2、淺拷貝的實(shí)現(xiàn)

(1)Object.assign()

Object.assign()方法可以把源對象自身的任意多個的可枚舉屬性拷貝給目標(biāo)對象,然后返回目標(biāo)對象,但是Object.assign()進(jìn)行的是淺拷貝,拷貝的是對象的屬性的引用,而不是對象本身。此方法對于Array和Object均可適用。

var obj1 = {
  'name' : 'zhangsan',
  'language' : [1,[2,3],[4,5]],
};
var obj2 = Object.assign({}, obj1);
obj2.name = "lisi";
obj2.language[1] = ["二","三"];
console.log('obj1',obj1)
console.log('obj2',obj2)

(2)Array.prototype.concat()和Array.prototype.slice()

Array.prototype.concat()和Array.prototype.slice()均為Array原型上的方法,只適用于Array。

var arr1 = [1,3,{
 user: 'aaa'
}]
var arr2 = arr1.concat();
arr2[0] = '一';
arr2[2].user = 'AAA';
console.log('arr1',arr1)
console.log('arr2',arr2)


var arr1 = [1,3,{
 user: 'aaa'
}]
var arr2 = arr1.slice();
arr2[0] = '一';
arr2[2].user = 'AAA';
console.log('arr1',arr1)
console.log('arr2',arr2)

補(bǔ)充說明:Array的slice和contact方法都不會修改原數(shù)組,而是會返回一個對原數(shù)組進(jìn)行淺拷貝的新數(shù)組。這兩種方法同Object.assign()一樣,都是對第一層屬性依次拷貝,如果第一層的屬性是基本數(shù)據(jù)類型,就拷貝值;如果是引用數(shù)據(jù)類型,就拷貝內(nèi)存地址。

三、JS深拷貝

對對象的屬性中所有引用類型的值,遍歷到是基本類型的值為止。

3.1、深拷貝實(shí)現(xiàn)方式

(1)JSON.parse(JSON.stringify())

原理:用JSON.stringify()將對象轉(zhuǎn)成字符串,再用JSON.parse()把字符串解析成對象。

var obj1 = {
  'name' : 'zhangsan',
  'language' : [1,[2,3],[4,5]],
};
var obj2 = JSON.parse(JSON.stringify(obj1));
obj2.name = "lisi";
obj2.language[1] = ["二","三"];
console.log('obj1',obj1)
console.log('obj2',obj2)

缺點(diǎn):這種方法可以實(shí)現(xiàn)數(shù)組和對象和基本數(shù)據(jù)類型的深拷貝,但不能處理函數(shù)。因?yàn)镴SON.stringify()方法是將一個javascript值轉(zhuǎn)換我一個JSON字符串,不能接受函數(shù)。其他影響如下:

  • 如果對象中有時間對象,那么用該方法拷貝之后的對象中,時間是字符串形式而不是時間對象
  • 如果對象中有RegExp、Error對象,那么序列化的結(jié)果是空
  • 如果對象中有函數(shù)或者undefined,那么序列化的結(jié)果會把函數(shù)或undefined丟失
  • 如果對象中有NAN、infinity、-infinity,那么序列化的結(jié)果會變成null
  • JSON.stringfy()只能序列化對象的可枚舉自有屬性,如果對象中有是構(gòu)造函數(shù)生成的,那么拷貝后會丟棄對象的constructor
  • 如果對象中存在循環(huán)引用也無法正確實(shí)現(xiàn)深拷貝

(2)手寫深拷貝函數(shù)

通過遞歸實(shí)現(xiàn)深拷貝

function deepCopy(obj){
 var result= Array.isArray(obj) ? [] : {}
 if (obj && typeof(obj) === 'object') {
  for (let i in obj) {
   if (obj.hasOwnProperty(i)){ // 思考:這句是否有必要?
    if (obj[i] && typeof(obj[i]) === 'object') {
     result[i] = deepCopy(obj[i])
    } else {
     result[i] = obj[i]
    }
   }
  }
 }
 return result
}
var obj1 = {
 a: 1,
 b: {
  c: 2
 }
};
var obj2 = deepCopy(obj1);
obj2.a = '一';
obj2.b.c = '二'
console.log('obj1', obj1)
console.log('obj2', obj2)

obj.hasOwnProperty(prop)用來判斷obj這個對象中是否含有prop這個屬性,返回布爾值,有則true,沒有則false

以上有個缺陷:當(dāng)遇到兩個互相引用的對象時,會出現(xiàn)死循環(huán)的情況,從而導(dǎo)致爆棧。為了避免相互引用的對象導(dǎo)致死循環(huán)的情況,則應(yīng)該在遍歷的時候判斷是否互相引用。

深拷貝函數(shù)改進(jìn)(防止循環(huán)遞歸爆棧)

function deepCopy(obj, parent = null) {
 let result = Array.isArray(obj) ? [] : {}
 let _parent = parent
 // 該字段有父級則需要追溯該字段的父級
 while(_parent) {
  // 如果該字段引用了它的父級,則為循環(huán)引用
  if (_parent.originalParent === obj) {
   // 循環(huán)引用返回同級的新對象
   return _parent.currentParent 
  }
  _parent = _parent.parent
 }
 if (obj && typeof(obj) === 'object') {
  for (let i in obj) {
   // 如果字段的值也是一個對象
   if (obj[i] && typeof(obj[i]) === 'object') {
    // 遞歸執(zhí)行深拷,將同級的待拷貝對象傳遞給parent,方便追溯循環(huán)引用
    result[i] = deepCopy(obj[i], {
     originalParent: obj,
     currentParent: result,
     parent: parent
    })
   } else {
    result[i] = obj[i]
   }
  }
 }
 return result
}
var obj1 = {
 x: 1,
 y: 2
};
obj1.z = obj1
var obj2 = deepCopy(obj1)
console.log('obj1', obj1)
console.log('obj2', obj2)

以上代碼可以復(fù)制到瀏覽器去試試吧

深拷貝函數(shù)最終版(支持基本數(shù)據(jù)類型、Array、Object、原型鏈、RegExp、Date類型)

function deepCopy(obj, parent = null) {
 let result
 let _parent = parent
 while(_parent) {
  if (_parent.originalParent === obj) {
   return _parent.currentParent
  }
  _parent = _parent.parent
 }
 if (obj && typeof(obj) === 'object') {
  if (obj instanceof RegExp) {
   result = new RegExp(obj.source, obj.flags)
  } else if (obj instanceof Date) {
   result = new Date(obj.getTime())
  } else {
   if (obj instanceof Array) {
    result = []
   } else {
    let proto = Object.getPrototypeOf(obj)
    result = Object.create(proto)
   }
   for (let i in obj) {
    if(obj[i] && typeof(obj[i]) === 'object') {
     result[i] = deepCopy(obj[i], {
      originalParent: obj,
      currentParent: result,
      parent: parent
     })
    } else {
     result[i] = obj[i]
    }
   }
  }
 } else {
  return obj
 }
 return result
}
var obj1 = {
 x: 1 
}

//試調(diào)用
function construct(){
  this.a = 1,
  this.b = {
    x:2,
    y:3,
    z:[4,5,[6]]
  },
  this.c = [7,8,[9,10]],
  this.d = new Date(),
  this.e = /abc/ig,
  this.f = function(a,b){
    return a+b
  },
  this.g = null,
  this.h = undefined,
  this.i = "hello",
  this.j = Symbol("foo")
}
construct.prototype.str = "I'm prototype"
var obj1 = new construct()
obj1.k = obj1
obj2 = deepCopy(obj1)

obj2.b.x = 999
obj2.c[0] = 666

console.log('obj1', obj1)
console.log('obj2', obj2)

(3)函數(shù)庫

也可以使用一些函數(shù)庫,比如函數(shù)庫lodash,也有提供_.cloneDeep用來做深拷貝;

var _ = require('lodash');
var obj1 = {
  a: 1,
  b: { f: { g: 1 } },
  c: [1, 2, 3]
};
var obj2 = _.cloneDeep(obj1);
console.log(obj1.b.f === obj2.b.f);
// false

參考

http://chabaoo.cn/article/181898.htm

http://chabaoo.cn/article/140928.htm

以上就是詳解JS深拷貝與淺拷貝的詳細(xì)內(nèi)容,更多關(guān)于JS深拷貝與淺拷貝的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論