当前位置:网站首页>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 language string function: strlen, strcpy, strcat

First acquaintance with C language - first acquaintance with pointer

「PHP基础知识」整型数据的使用

Configure the route and go to the login home page to send the request

初识C语言——常见的数据类型

攻防世界-mfw

路由的配置和前往登录首页进行发送请求

Share a multiple-choice question about define (including the replacement rules, program environment and preprocessing related knowledge of define during precompiling)
![[BJDCTF2020]EasySearch 1](/img/ea/90ac6eab32c28e09bb1fab62b6b140.png)
[BJDCTF2020]EasySearch 1

Initial C language -- the function of keyword static
随机推荐
==,===,Object.is
分享一道关于程序编译过程的选择题(内含编译过程浅谈,符号表的形成合并过程)
Pytorch installation new pit
[CISCN2019 华东南赛区]Web11 1
初识C语言——字符串+转义字符+注释
JS基础知识--每日学习总结①
JS中arguments类数组
Source code of document type full-text retrieval knowledge base management system
C language string function: StrCmp, strncpy, strncat, strncmp, strstr, strtok, strError
JS中&&(与)和||(或)操作符的返回值
流程控制-分支
分享一道关于#define的选择题(内含#define在预编译时的替换规则,程序环境和预处理相关知识)
sql_mode严格模式(ANSI/TRADITIONAL/STRICT_TRANS_TABLES)
User login - and create and verify SMS verification code
SQL (MySQL) rookie tutorial knowledge
页面的配置
store redux在项目中的应用
C语言进制转换以及原补反码位运算介绍
[极客大挑战 2019]FinalSQL 1
GCC compilation options