当前位置:网站首页>[JS] shallow copy and deep copy
[JS] shallow copy and deep copy
2022-07-20 08:48:00 【one or only】
One 、 understand
1、 Shallow copy : It's just Copy a layer of , Deeper object level Only the address was copied .
When copying shallowly, if the data is the basic data type , So it's like a direct assignment , Will copy itself , If there is a layer of objects besides the basic data types , So for For shallow copy, only its references can be copied , The change of the object will be reflected on the copied object .
2、 Deep copy : Deep copy will Copy multiple layers , Even if the objects are nested , They will all be copied out , The content is the same as the original object , Change the original object , The copy object will not change .
Two 、 Shallow copy
1. use Js Implement shallow copy
var obj = {
id:1,
name:'Andy',
msg: {
age:18
}
}
var newObj = {}
for(var key in obj) {
//key Is the current property name , obj[k] Is the current attribute value
newObj[key] = obj[key]
}
console.log(newObj)
From the print results, we can see that obj The attribute value of has been copied into newObj, But we should realize :obj.msg.age And newObj.msg.age They are quoting data from the same address , let me put it another way ,newObj.msg Only copies obj.msg The address of .
2. Light copy grammar sugar
ES6 The new method assign
It can quickly realize shallow copy
2.1 How to write it
Object.assign(target, ...sources)
- target: To whom
- source: Objects to copy
2.2 application
var obj = {
id:1,
name:'Andy',
msg: {
age:18
}
}
var newObj = {}
Object.assign(newObj,obj) // hold obj Shallow copy to newObj
summary : Shallow copy if you encounter deeper data ( Such as : object 、 Reference type data such as arrays ), Just copied the address .
3、 ... and 、 Deep copy
1. use Js Implement deep copy
var obj = {
id:1,
name:'Andy',
msg: {
age:18
},
color:['pink','red']
}
var newObj = {}
// Packaging function
function deepCopy(newObj,obj) {
for(var key in obj) {
// Determine which data type the attribute value belongs to
// Property value obj[key]
//1. Determine whether this value is an array ( Arrays are also special objects , It is also reference type data )
if(obj[key] instanceof Array) {
newObj[key] = []
deepCopy(newObj[key],obj[key]) // Using recursion , Give the attribute value of the original object to the new object
// Judge whether this value is an object
} else if(obj[key] instanceof Object) {
newObj[key] = {}
deepCopy(newObj[key],obj[key]) // Using recursion , Give the attribute value of the original object to the new object
} else {
// If it is a normal data type
newObj[key] = obj[key]
}
}
}
deepCopy(newObj,obj)
Traverse the original object , Check what data type each attribute value belongs to : if Simple data type , Direct assignment ; if Complex data type Of ( Array 、 object ), Let's first judge which complex data type it belongs to , Then we go inside and use recursive The way , Take out the value inside and assign it .
It is worth mentioning that : Determine whether the condition is an array 、 Whether it is an object or not, the two cannot be reversed , Because if you first judge whether it is an object , Then the array will also enter the judgment , Because the array is “ Special objects ”.
边栏推荐
- 【测试技术-自动化测试-pytest】Pytest基础总结
- php生成的csv, 无法完整显示带前导0的数字
- 【js】call()、apply()、bind() 的用法
- 【论文】MEC: Memory-efficient Convolution for Deep Neural Network
- WordPress函数大全,学会了你就可以做主题了
- 83 reuse of local components [parent to child]
- [Database Foundation] MySQL foundation summary 2
- 1. Realize MVVM bidirectional data binding stepping pit - infinite trigger get, set
- ThinkPHP5 验证码
- VS2019修改背景+高度自定义字体颜色
猜你喜欢
[learning notes] "machine room reservation system" operation replay and problem summary
7.Object.defineProperty以及数据代理(数据劫持)
10C polkadot substrate : 添加 pallet 到 runtime
15.内置指令
Wordpress固定链接怎么设置伪静态
文件包含漏洞
83 reuse of local components [parent to child]
Space saving "bit segments" in C language“
74-学生管理系统-添加-删除-展示
【数据库基础】MySql基础总结
随机推荐
Wordpress固定链接怎么设置伪静态
Supplement: ES6 knowledge points
Page search highlighting function, the page automatically matches keywords, scrolls to the middle of the screen, and highlights.
函数的定义方式和调用
性能测试基础笔试题
什么是三方支付?
mysql聚簇索引和非聚簇索引的区别
数据代理理解
Easily master the library functions related to C language string and memory|
防抖和节流
【测试技术-性能测试-Loadrunner】Loadrunner常用函数详解
页面搜索高亮功能,页面自动匹配关键字滚动到屏幕正中间,并高亮显示。
15.内置指令
正则表达式(用户名表单验证/验证座机号码/正则替换replace)
淘宝搜索案例
[paper] mec: memory efficient revolution for deep neural network
CuteOne:一款OneDrive多网盘挂载程序/带会员/同步等功能
继承(子构造函数继承父构造函数中的属性)
52-localStorage本地存储
家庭亲戚关系计算器微信小程序源码