当前位置:   article > 正文

浅拷贝与深拷贝的使用场景

深拷贝的使用场景

一. 浅拷贝

let arr=[1,2,3,4]

let getData=arr;

getData.splice(0,1);//当强制删除其中某一个值时arr也被删除了

console.log(arr);//[2,3,4]

console.log(getData);//[2,3,4]

//问题:修改getData数组时,arr的数组不发生变化

let getData=Object.assign([],arr);

getData.splice(0,1)

console.log(arr);//[1,2,3,4]

console.log(getData);//[2,3,4]

二.深拷贝当一个数组中的对象发生变化时,则被建立引用关系接收的值也会发生变化

  1. //当一个数组中的对象发生变化时,则被建立引用关系接收的值也会发生变化
  2. handlePClickBefore(valOut){
  3. console.log('传进来的下标');
  4. console.log(valOut.sortId);
  5. //获取上一级的下标的内容,preLevel接收此对象的值建立了引用关系,
  6. 当所引用的对象this.controllerZyCount发生变化时
  7. preLevel也会发生变化
  8. let preLevel= this.controllerZyCount[valOut.sortId-1-1];
  9. //获取当前的下标的内容
  10. let getObject=this.controllerZyCount[valOut.sortId-1];
  11. this.controllerZyCount.splice(valOut.sortId-1,1, preLevel );
  12. this.controllerZyCount.splice(valOut.sortId-1-1,1,getObject);
  13. console.log('插入后');
  14. console.log(this.controllerZyCount);
  15. console.log(getObject['zyName']);
  16. //此时preLevel的值是正常的
  17. console.log( preLevel ['zyName']);
  18. console.log(this.controllerZyCount[valOut.sortId - 1]['zyName']);
  19. console.log(this.controllerZyCount[valOut.sortId - 1 - 1]['zyName']);
  20. this.controllerZyCount[valOut.sortId-1]['zyName']=getObject['zyName'];
  21. this.controllerZyCount[valOut.sortId-1]['sortId']=getObject['sortId'];
  22. //等到修改controllerZyCount[valOut.sortId-1]中的值时,preLevel发生了变化
  23. console.log( preLevel ['zyName']);
  24. this.controllerZyCount[valOut.sortId-1-1]['zyName']=preLevel.zyName;
  25. this.controllerZyCount[valOut.sortId-1-1]['sortId']= preLevel.sortId;
  26. },

2.解决以上问题的方式用深拷贝解决

  1. //上移
  2. handlePClickBefore(valOut){
  3. console.log('传进来的下标');
  4. console.log(valOut.sortId);
  5. //获取上一级的下标的内容
  6. let preLevel= JSON.stringify(this.controllerZyCount[valOut.sortId-1-1]);
  7. // let preLevelSortId=this.controllerZyCount[valOut.sortId-1-1]['sortId'];
  8. // let preLevelZyName=this.controllerZyCount[valOut.sortId-1-1]['zyName'];
  9. //获取当前的下标的内容
  10. let getObject=this.controllerZyCount[valOut.sortId-1];
  11. this.controllerZyCount.splice(valOut.sortId-1,1,JSON.parse(preLevel));
  12. this.controllerZyCount.splice(valOut.sortId-1-1,1,getObject);
  13. console.log('插入后');
  14. console.log(this.controllerZyCount);
  15. console.log(getObject['zyName']);
  16. console.log(JSON.parse(preLevel)['zyName']);
  17. console.log(this.controllerZyCount[valOut.sortId - 1]['zyName']);
  18. console.log(this.controllerZyCount[valOut.sortId - 1 - 1]['zyName']);
  19. this.controllerZyCount[valOut.sortId-1]['zyName']=getObject['zyName'];
  20. this.controllerZyCount[valOut.sortId-1]['sortId']=getObject['sortId'];
  21. console.log(JSON.parse(preLevel)['zyName']);
  22. this.controllerZyCount[valOut.sortId-1-1]['zyName']=JSON.parse(preLevel).zyName;
  23. this.controllerZyCount[valOut.sortId-1-1]['sortId']=JSON.parse(preLevel).sortId;
  24. this.$set(this.controllerZyCount);
  25. console.log('赋值后');
  26. console.log(this.controllerZyCount);
  27. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/68428
推荐阅读
相关标签
  

闽ICP备14008679号