您好,登錄后才能下訂單哦!
這篇文章主要介紹“JS怎么實(shí)現(xiàn)深拷貝和淺拷貝”,在日常操作中,相信很多人在JS怎么實(shí)現(xiàn)深拷貝和淺拷貝問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JS怎么實(shí)現(xiàn)深拷貝和淺拷貝”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
說道數(shù)據(jù)拷貝就離不開數(shù)據(jù)類型,在JS中數(shù)據(jù)類型分為基本類型和引用類型 基本類型:
number, boolean,string,symbol,bigint,undefined,null
引用類型:
object 以及一些標(biāo)準(zhǔn)內(nèi)置對象 Array、RegExp、String、Map、Set..
基本類型數(shù)據(jù)都是值類型,存儲(chǔ)在棧內(nèi)存中,每次賦值都是一次復(fù)制的過程
var a = 12; var b = a;
只拷貝對象的一層數(shù)據(jù),再深處層次的引用類型value將只會(huì)拷貝引用 實(shí)現(xiàn)方式:
1.Object.assign() 和 ES6的拓展運(yùn)算符
通常我們用 Object.assign() 方法來實(shí)現(xiàn)淺拷貝。 Object.assign()用于將所有可枚舉屬性的值從一個(gè)或多個(gè)源對象分配到目標(biāo)對象。它將返回目標(biāo)對象。
let aa = { a: undefined, func: function(){console.log(1)}, b:2, c: {x: 'xxx', xx: undefined}, d: null, e: BigInt(100), f: Symbol('s') } let bb = Object.assign({}, aa) // 或者 let bb = {...aa} aa.c.x = 111 console.log(bb) // 第一層拷貝,遇到引用類型的值就會(huì)只拷貝引用 // { // a: undefined, // func: [Function: func], // b: 2, // c: { x: 111, xx: undefined }, // d: null, // e: 100n, // f: Symbol(s) // }
2.Object.create
Object.create()方法創(chuàng)建一個(gè)新對象,使用現(xiàn)有的對象來提供新創(chuàng)建的對象的__proto__。Object.create(proto,[propertiesObject])接收兩個(gè)參數(shù)一個(gè)是新創(chuàng)建對象的__proto__, 一個(gè)屬性列表
let aa = { a: undefined, func: function(){console.log(1)}, b:2, c: {x: 'xxx', xx: undefined}, } let bb = Object.create(aa, Object.getOwnPropertyDescriptors(aa)) aa.c.x = 111 console.log(bb) // 第一層拷貝,遇到引用類型的值就會(huì)只拷貝引用 // { // a: undefined, // func: [Function: func], // b: 2, // c: { x: 111, xx: undefined }, // }
在拷貝一個(gè)對象的時(shí)候?yàn)榱吮苊庑薷膶?shù)據(jù)造成的影響,必須使用深拷貝。
實(shí)現(xiàn)方式:
1、 通過JSON.stringify()
var a = {a:1, b: 2} var b = JSON.stringify(a); a.a = 'a' console.log(a, b) // { a: 'a', b: 2 } {"a":1,"b":2}
JSON.stringify()進(jìn)行深拷貝有弊端: 忽略value為function, undefind, symbol, 并且在序列化BigInt時(shí)會(huì)拋出語法錯(cuò)誤:TypeError: Do not know how to serialize a BigInt
// 序列化function, undefind, symbol,忽略-------------------------------------------------------- var obj = { a:function(){}, b: undefined, c: null, d: Symbol('s'), } var objCopyed = JSON.stringify(obj); console.log("a:", a) // obj: { a: [Function: a], b: undefined, c: null, d: Symbol(s) } console.log("objCopyed:", objCopyed) // objCopyed: {"c":null} // 序列化bigint拋出錯(cuò)誤-------------------------------------------------------- var obj = { a: 1, e: BigInt(9007199254740991) } var objCopyed = JSON.stringify(obj); // TypeError: Do not know how to serialize a BigInt
2、遞歸實(shí)現(xiàn)
const deepCopy = (obj) => { // 優(yōu)化 把值類型復(fù)制方放到這里可以少一次deepCopy調(diào)用 // if(!obj || typeof obj !== 'object') throw new Error("請傳入非空對象") if(!obj || typeof obj !== 'object') return obj let result = {} if (Object.prototype.toString.call(obj).indexOf('Array') > 0) { result = [] } // 另一種循環(huán)方式 // for (let key in obj) { // if (obj.hasOwnProperty(key)) { // result[key] = deepClone(obj[key]) // } // } Object.keys(obj).forEach(key => { // 優(yōu)化 把值類型復(fù)制方放到這里可以少一次deepCopy調(diào)用 // if (obj[key] && typeof obj[key] === 'object') { // result[key] = deepCopy(obj[key]) // }else{ // result[key] = obj[key] // } result[key] = deepCopy(obj[key]) }); return result } let aa = { a: undefined, func: function(){console.log(1)}, b:2, c: {x: 'xxx', xx: undefined}, d: null, e: BigInt(100), f: Symbol('s') } let bb = deepCopy(aa) aa.c.x = 123 aa.func = {} console.log("aa", aa) console.log("bb", bb) // aa { // a: undefined, // func: {}, // b: 2, // c: { x: 123, xx: undefined }, // d: null, // e: 100n, // f: Symbol(s) // } // bb { // a: undefined, // func: [Function: func], // b: 2, // c: { x: 'xxx', xx: undefined }, // d: null, // e: 100n, // f: Symbol(s) // }
到此,關(guān)于“JS怎么實(shí)現(xiàn)深拷貝和淺拷貝”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。