当前位置:网站首页>The difference between deep copy and shallow copy
The difference between deep copy and shallow copy
2022-06-12 08:37:00 【Month to month better】
1、 Shallow copy
Shallow copy copies only the reference of an object , Instead of copying the object itself , New and old objects still share the same block of memory
2、 Deep copy
Deep copy creates an identical object , The new object and the original object do not share memory , Modifying the new object will not change the original pair of objects .
Two 、 Method
1、 Shallow copy
a、Object.assign()
Object.assign() Method can copy the enumerable properties of any multiple source objects to the target objects , Then return the target object .Object.assign() Copy the reference of the object's properties , Not the object itself .
2、 Deep copy
a、JSON transformation
let newObj = JSON.parse(JSON.stringify(obj));
shortcoming :
- If the object has a function , Functions cannot be copied
2) Cannot copy properties and methods on the object prototype chain
3) When the level of data is very deep , Stack overflow
b、 Recursive function
function deepClone(obj){
let tempObj = obj.constructor ===Array ? [] : {
}
for (let keys in obj) {
if(obj.hasOwnProperty(keys)) {
if (obj[keys] && typeof obj[keys] === "object") {
tempObj[keys] = deepClone(obj[keys])
} else {
tempObj[keys] = obj[keys]
}
}
}
return tempObj
}
shortcoming :
1) Unable to save reference
2) When the data level is very deep , Stack overflow
边栏推荐
- Engineers learn music theory (II) scale and tendency
- Shell basic syntax -- array
- Callback webrtc underlying logs to the application layer
- Bean的作用域
- 三国杀周边--------猪国杀题解
- Beidou satellite navigation system foundation part 1
- Hands on learning and deep learning -- a brief introduction to softmax regression
- MYSQL中的查询
- Engineers learn music theory (I) try to understand music
- 【动态内存管理】malloc&calloc和realloc和笔试题和柔性数组
猜你喜欢

Regular expressions in JS

JVM学习笔记:三 本地方法接口、执行引擎

Oracle installation details (verification)

(p36-p39) right value and right value reference, role and use of right value reference, derivation of undetermined reference type, and transfer of right value reference

Hands on learning and deep learning -- simple implementation of linear regression

Hands on deep learning -- Introduction to linear regression model

ctfshow web 1-2

Error: what if the folder cannot be deleted when it is opened in another program

深拷贝与浅拷贝的区别

网站Colab与Kaggle
随机推荐
动态线段树leetcode.699
In the era of intelligent manufacturing, how do enterprises carry out digital transformation
智能制造的时代,企业如何进行数字化转型
Hands on deep learning -- weight decay and code implementation
Centso8 installing mysql8.0 (Part 2)
ctfshow web 1-2
【新规划】
What scheduling rules does the APS software have? What is the exception handling scheme?
Scope of bean
When converting tensor to ndarray in tensorflow, the run or Eval function is constantly called in the loop, and the code runs more and more slowly!
Hands on learning and deep learning -- simple implementation of linear regression
(p19-p20) delegate constructor (proxy constructor) and inheritance constructor (using)
ctfshow web3
Ankerui fire emergency lighting and evacuation indication system
(p40-p41) transfer and forward perfect forwarding of move resources
Error: what if the folder cannot be deleted when it is opened in another program
Hands on learning and deep learning -- Realization of linear regression from scratch
Website colab and kaggle
Bean的作用域
Loading circling effect during loading