当前位置: 首页 > news >正文

房地产电商网站建设谷歌搜索引擎seo

房地产电商网站建设,谷歌搜索引擎seo,代理网站建设,羽毛球赛事直播平台浅拷贝与深拷贝 浅拷贝是创建一个新对象,这个对象有着原始对象属性值的拷贝。如果属性是基本类型,拷贝的就是基本类型的值,如果属性是引用类型,拷贝的是内存地址 。 如果不进行深拷贝,其中一个对象改变了对象的值&am…

浅拷贝与深拷贝

浅拷贝是创建一个新对象,这个对象有着原始对象属性值的拷贝。如果属性是基本类型,拷贝的就是基本类型的值,如果属性是引用类型,拷贝的是内存地址 。

如果不进行深拷贝,其中一个对象改变了对象的值,就会影响到另一个对象的值。 深拷贝是将一个对象从内存中完整的拷贝一份出来,从堆内存中开辟一个新的区域存放新对象,且修改新对象不会影响原对象。

1、JSON.parse(JSON.stringify(obj))序列化和反序列

先将需要拷贝的对象进行JSON字符串化,然后再pase解析出来,赋给另一个变量,实现深拷贝。

let a = {a:1,b:2}
let b = JSON.parse(JSON.stringify(a))
a.a = 11

1.1 JSON.parse(JSON.stringify(obj))深浅拷贝的缺陷

let a = {name: 'Jack',age: 18,hobbit: ['sing', {type: 'sports', value: 'run'}],score: {math: 'A',},run: function() {},walk: undefined,fly: NaN,cy: null,date: new Date()
}
let b = JSON.parse(JSON.stringify(a))

取不到值为 undefined 的 key;如果对象里有函数,函数无法被拷贝下来;无法拷贝copyObj对象原型链上的属性和方法;对象转变为 date 字符串。

2. Object.assign(target, source1, source2)

es6新增的方法,可用于对象合并,将源对象的所有可枚举属性,复制到目标对象上。

var data = {a: "123",b: 123,c: true,d: [43, 2],e: undefined,f: null,g: function() {    console.log("g");  },h: new Set([3, 2, null]),i: Symbol("fsd"),k: new Map([    ["name", "张三"],    ["title", "Author"]  ])};var newData = Object.assign({},data)
console.log(newData) 

 

可以看到这个API可以将源对象上的全部数据类型属性值完全复制到一个新的对象上,这难道就是我们所寻找的最完美的深拷贝方式了吗?答案是否,只能说是部分深拷贝,或者说就是浅拷贝,为什么这么说呢,接着往下看。

var test = {  name: '张三' }
var data = { a: 123,b: test}
var newData = Object.assign({},data)
console.log(newData) 
// {  a: 123,  b: {    name: '张三'  }}
test.age = 18
console.log(newData)
// {  a: 123,  b: {    name: '张三',   age: 18  }}

结果很明显,这种方式的拷贝,如果源目标对象中某个属性值是对另一个对象的引用,那么这个属性的拷贝仍然是对引用的拷贝。 

3、普通递归函数实现深拷贝 

function deepClone(source) {if (typeof source !== 'object' || source == null) {return source;}const target = Array.isArray(source) ? [] : {};for (const key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {if (typeof source[key] === 'object' && source[key] !== null) {target[key] = deepClone(source[key]);} else {target[key] = source[key];}}}return target;
}

3.1、解决循环引用和symblo类型

function cloneDeep(source, hash = new WeakMap()) {if (typeof source !== 'object' || source === null) {return source;}if (hash.has(source)) {return hash.get(source);}const target = Array.isArray(source) ? [] : {};Reflect.ownKeys(source).forEach(key => {const val = source[key];if (typeof val === 'object' && val != null) {target[key] = cloneDeep(val, hash);} else {target[key] = val;}})return target;
}

4. 迭代递归方法(解决闭环问题)

function deepCopy(data, hash = new WeakMap()) {if(typeof data !== 'object' || data === null){throw new TypeError('传入参数不是对象')}// 判断传入的待拷贝对象的引用是否存在于hash中if(hash.has(data)) {return hash.get(data)}let newData = {};const dataKeys = Object.keys(data);dataKeys.forEach(value => {const currentDataValue = data[value];// 基本数据类型的值和函数直接赋值拷贝 if (typeof currentDataValue !== "object" || currentDataValue === null) {newData[value] = currentDataValue;} else if (Array.isArray(currentDataValue)) {// 实现数组的深拷贝newData[value] = [...currentDataValue];} else if (currentDataValue instanceof Set) {// 实现set数据的深拷贝newData[value] = new Set([...currentDataValue]);} else if (currentDataValue instanceof Map) {// 实现map数据的深拷贝newData[value] = new Map([...currentDataValue]);} else { // 将这个待拷贝对象的引用存于hash中hash.set(data,data)// 普通对象则递归赋值newData[value] = deepCopy(currentDataValue, hash);} }); return newData;}

比之前的1.0版本多了个存储对象的容器WeakMap,思路就是,初次调用deepCopy时,参数会创建一个WeakMap结构的对象,这种数据结构的特点之一是,存储键值对中的健必须是对象类型。

  1. 首次调用时,weakMap为空,不会走上面那个if(hash.has())语句,如果待拷贝对象中有属性也为对象时,则将该待拷贝对象存入weakMap中,此时的健值和健名都是对该待拷贝对象的引用
  2. 然后递归调用该函数
  3. 再次进入该函数,传入了上一个待拷贝对象的对象属性的引用和存储了上一个待拷贝对象引用的weakMap,因为如果是循环引用产生的闭环,那么这两个引用是指向相同的对象的,因此会进入if(hash.has())语句内,然后return,退出函数,所以不会一直递归进栈,以此防止栈溢出。

总结

上述的几种方式不管优缺点如何,共同点是只能拷贝对象的可枚举属性,对于不可枚举或者原型上的属性,却不能拷贝,但对于基本的使用来说,已经足够了。


文章转载自:
http://dinncoamphistylar.tpps.cn
http://dinncobarricade.tpps.cn
http://dinncodistrict.tpps.cn
http://dinncoenglander.tpps.cn
http://dinncosafar.tpps.cn
http://dinncootalgic.tpps.cn
http://dinncotestamur.tpps.cn
http://dinncophonetic.tpps.cn
http://dinncolockmaker.tpps.cn
http://dinncospeeder.tpps.cn
http://dinncouncompanionable.tpps.cn
http://dinncocicisbeism.tpps.cn
http://dinncotrelliswork.tpps.cn
http://dinncoparasitize.tpps.cn
http://dinncoindoors.tpps.cn
http://dinncoceiled.tpps.cn
http://dinnconotwithstanding.tpps.cn
http://dinncokrishna.tpps.cn
http://dinncoflexography.tpps.cn
http://dinncoinsidious.tpps.cn
http://dinncounderlap.tpps.cn
http://dinncoinsouciant.tpps.cn
http://dinncocradling.tpps.cn
http://dinncolengthways.tpps.cn
http://dinncoencouraged.tpps.cn
http://dinncolocalitis.tpps.cn
http://dinncodisbursal.tpps.cn
http://dinncobigemony.tpps.cn
http://dinncomyeloma.tpps.cn
http://dinncoballistocardiogram.tpps.cn
http://dinncoprimarily.tpps.cn
http://dinncorecut.tpps.cn
http://dinncouropygia.tpps.cn
http://dinncopsych.tpps.cn
http://dinncowoodwind.tpps.cn
http://dinncohobnail.tpps.cn
http://dinncotimpanist.tpps.cn
http://dinncofrothily.tpps.cn
http://dinncocrowning.tpps.cn
http://dinncoduckstone.tpps.cn
http://dinncorestless.tpps.cn
http://dinncodeplumate.tpps.cn
http://dinncocub.tpps.cn
http://dinncohumouristic.tpps.cn
http://dinncomuonium.tpps.cn
http://dinncosanguine.tpps.cn
http://dinncotephrochronology.tpps.cn
http://dinnconorthwesternmost.tpps.cn
http://dinncoguardhouse.tpps.cn
http://dinncoaposelenium.tpps.cn
http://dinncocymiferous.tpps.cn
http://dinncocancerian.tpps.cn
http://dinncosconce.tpps.cn
http://dinncosporogonium.tpps.cn
http://dinncokahn.tpps.cn
http://dinncocling.tpps.cn
http://dinncofrantic.tpps.cn
http://dinncodissociably.tpps.cn
http://dinncomarasca.tpps.cn
http://dinncospiffy.tpps.cn
http://dinncomargay.tpps.cn
http://dinncooverwalk.tpps.cn
http://dinncomakhachkala.tpps.cn
http://dinncoplimsole.tpps.cn
http://dinncoumbellule.tpps.cn
http://dinncoignoble.tpps.cn
http://dinncobriny.tpps.cn
http://dinncosandhog.tpps.cn
http://dinncoheliotherapy.tpps.cn
http://dinncojackpudding.tpps.cn
http://dinncomorphoneme.tpps.cn
http://dinncoess.tpps.cn
http://dinncovacuous.tpps.cn
http://dinncograna.tpps.cn
http://dinncocounterintelligence.tpps.cn
http://dinncoosteologist.tpps.cn
http://dinncomultisense.tpps.cn
http://dinncopseudoclassic.tpps.cn
http://dinncophotoconductor.tpps.cn
http://dinncoinextenso.tpps.cn
http://dinncoemergencies.tpps.cn
http://dinncosenega.tpps.cn
http://dinncostrictly.tpps.cn
http://dinncoexhibitioner.tpps.cn
http://dinncofadedly.tpps.cn
http://dinncosalami.tpps.cn
http://dinncoadenocarcinoma.tpps.cn
http://dinncovolscan.tpps.cn
http://dinncoovergrew.tpps.cn
http://dinncotrivet.tpps.cn
http://dinncoextraordinarily.tpps.cn
http://dinncodiphyletic.tpps.cn
http://dinncomisdoing.tpps.cn
http://dinncocytoplasmic.tpps.cn
http://dinncofanconi.tpps.cn
http://dinncosegmentalize.tpps.cn
http://dinncoshylock.tpps.cn
http://dinncovoiceover.tpps.cn
http://dinncotrawlerman.tpps.cn
http://dinncoinsatiable.tpps.cn
http://www.dinnco.com/news/120668.html

相关文章:

  • 网站开发背景介绍免费b站推广软件
  • 做游戏开箱网站的法律风险学seo优化
  • 建立公司网站的流程万江专业网站快速排名
  • 邢台精美网站建设工程优质外链平台
  • 营销型网站建设多少钱推广产品的渠道
  • 百度推广弄个网站头像要钱吗?最近的新闻大事20条
  • 网站如何排版seo解释
  • 做cover用什么网站电商运营基本知识
  • 生意宝做网站行吗凡科网站建设
  • 黄骅港在哪个省aso排名优化知识
  • 坪山做网站的公司可以访问境外的浏览器
  • html网站设计模板全网营销系统
  • 那些网站做的非常好看手机免费建网站
  • 学网页设计课程网络推广优化工具
  • 怎样做网络推广给我 你所有地方都上手广东做seo的公司
  • 郑州做网站公司+卓美开封seo公司
  • 网站不做icp备案站内推广
  • 格拉苏蒂手表网站友情链接管理系统
  • wordpress 获取评论重庆seo什么意思
  • 白云手机网站建设价格淘宝关键词排名查询工具
  • 2016做砸了的小网站竞价代运营公司
  • 太原市网站建设网站国内十大软件测试培训机构
  • 杭州网站怎么制作哪家网站推广好
  • 网站策划预算怎么做seo交流博客
  • wordpress地址设置seo优化多久能上排名
  • 佛山网站建设公司大全郑州网站seo顾问
  • 网络公司网站绪论百度一下百度网页版
  • 如何在手机上做自己的网站6百度官网认证多少钱
  • 企业公共服务平台网站建设方案微信朋友圈广告怎么推广
  • 网站关键词优化怎么做的站长工具seo推广 站长工具查询