当前位置:网站首页>The difference between deep copy and shallow copy in JS
The difference between deep copy and shallow copy in JS
2022-07-27 05:41:00 【weixin_ forty-six million fifty-one thousand two hundred and si】
It mainly depends on whether the copied new object and the original object will interact , Change one , Will the other change
difference
1) Shallow copy : It just points to the copied memory address , If the original address changes , Then the shallow copied objects will change accordingly
2) Deep copy : Create a new address in memory to store the copied object , Old and new objects do not share memory , Modify one of , The other will also be affected
Method
1) Shallow copy
- adopt = Direct assignment
- Object.assign( Target audience , Source object ) Method
2) Deep copy
- adopt JSON Object to implement deep copy :
*1----JSON.stringify: take js Value ( Object or array ) Turn to one JSON character string
*2----JSON.parse: Used to resolve JSON character string , Convert to Object type
var obj={
name:'jack',age:18}
var obj1=JSON.parse(JSON.stringify(obj))
obj1.age=20
console.log(obj);//{name:'jack',age:18}
console.log(obj1);//{nmae:'jack',age:20}
- Using extension operators for deep copy
var obj={
name:'bob',age:10}
var obj1={
...obj,age:20}
console.log(obj1);//{name:'bob',age:20}
边栏推荐
- 【C语言switch分支语句和循环语句】
- Source code of document type full-text retrieval knowledge base management system
- js中isNaN和Number.isNaN的区别
- [网鼎杯 2020 青龙组]AreUSerialz(BUUCTF)
- 洛谷超级玛丽游戏
- User page management
- cmd命令和npm命令
- 下载url-loader,用limit指定图片大小后,显示不出图片
- C language string function: strlen, strcpy, strcat
- Trying to evolve_ My first CSDN blog
猜你喜欢

Program environment and preprocessing (Part 1): how does a program run successfully?

分享一道关于#define的选择题(内含#define在预编译时的替换规则,程序环境和预处理相关知识)

初识C语言——常量、变量

布局的搭建及天气预报的显示

Edit delete user

Exit login and JSX display

维持登录,路由跳转

下载url-loader,用limit指定图片大小后,显示不出图片

C语言初阶——分支语句(if,switch)

First knowledge of C language -- common data types
随机推荐
Makefile is easy to understand and explain
First knowledge of C language -- common data types
First knowledge of C language - string + escape character + comment
【codeforces 1695C Zero Path】DP
XXE&XML 漏洞
[网鼎杯 2020 朱雀组]Nmap 1两种解法
弹性盒/伸缩盒(flex)的使用
[MRCTF2020]Ezpop 1
Introduction and management of heap memory in C language
cmd命令和npm命令
JS中深拷贝和浅拷贝的区别
初识C语言——什么是C语言
c语言字符串函数上:strlen、strcpy、strcat
初识C语言——常量、变量
js中isNaN和Number.isNaN的区别
一本通1251——仙岛求药(广度优先搜索)
页面的基本布局
Program environment and preprocessing (Part 1): how does a program run successfully?
C language makes a small maze
map结构