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

JS深浅拷贝

区分

B复制了A的值,如果A被修改,B的值也被改变,那就是浅拷贝。

如果B的值没有跟着修改,那就是深拷贝

深浅拷贝的方式

1、遍历赋值

2、Object.create()

3、JSON.parse()和JSON.stringify()

浅拷贝-遍历

    let a = {name:"jack",obj:{str:"ccc",arr:[1,2,3]},arr:[4,5,6]}let b = {};for(let i in a){b[i] = a[i];}
​// console.log(a);a.arr[1] = "aaa";console.log(b);

深拷贝-递归

   let a = {name:"jack",obj:{str:"ccc",arr:[1,2,3]},arr:[4,5,6]}
​function deepclone(startobj,endobj){let obj = endobj || {};for(let i in startobj){if(typeof startobj[i] === "object"){obj[i] = startobj[i].constractor === Array ? [] : {};deepclone(startobj[i],obj[i]);}else{obj[i] = startobj[i];}}return obj;}
​let b = deepclone(a);console.log(a);console.log(b);a.obj.str = "nerwwww";console.log(a);console.log(b);

深拷贝-JSON.parse、JSON.stringfy

    let a = {name:"jack",obj:{str:"ccc",arr:[1,2,3]},arr:[4,5,6]}
​let b = JSON.parse(JSON.stringify(a));
​console.log(a);console.log(b);a.obj.str = "nerwwww";console.log(a);console.log(b);

http://www.lryc.cn/news/267888.html

相关文章:

  • uni-app 命令行创建
  • ImageJ二值图像处理:形态学和分割
  • 自动驾驶中的“雷达”
  • Web 3.0 是什么
  • 四种NAT的网络结构
  • Android studio 使用greenDao根据实体类生成dao类
  • redis 从0到1完整学习 (九):SkipList 数据结构
  • 智能优化算法应用:基于金豺算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • Isaac Sim 仿真机器人urdf文件导入
  • Python 实现Excel和CSV之间的相互转换
  • 【GitHub精选项目】短信系统测试工具:SMSBoom 操作指南
  • 【Filament】立方体贴图(6张图)
  • SpringBoot 3.2.0 结合Redisson接入Redis
  • C++ 比C语言增加的新特性 5 之字符串string
  • 【第2讲】原理介绍和权限开通
  • C++ opencv-3.4.1 提取不规则物体的轮廓
  • ServletConfig对象.
  • jQuery实现框里画面的展开、收起和停止
  • less 查看文本时,提示may be a binary file.See it anyway?
  • H266/VVC帧内预测编码技术概述
  • 重组蛋白表达系统的比较-卡梅德生物
  • 【Java、Python】获取电脑当前网络IP进行位置获取(附源码)
  • 接口测试学习笔记
  • 一起玩儿物联网人工智能小车(ESP32)——14. 用ESP32的GPIO控制智能小车运动起来(二)
  • [PyTorch][chapter 8][李宏毅深度学习][DNN 训练技巧]
  • Nginx快速入门:实现企业安全防护|nginx部署https,ssl证书(七)
  • 将Go语言开发的Web程序部署到K8S
  • Python发送数据到Unity实现
  • Unity 渲染顺序受哪些影响(相机depth、SortingLayer、Render Queue、透明)
  • 【论文笔记】Run, Don’t Walk: Chasing Higher FLOPS for Faster Neural Networks