当前位置:网站首页>Mandatory interview questions: 1. shallow copy and deep copy_ Shallow copy
Mandatory interview questions: 1. shallow copy and deep copy_ Shallow copy
2022-07-24 23:08:00 【lazytomato】
Shallow copy
start
- In the process of working , Copy a copy of the data , Used for logical processing , It is a common scene .
- Take some time to summarize and learn relevant knowledge
assignment =
- Simple data type , assignment , It's about value
var a = ' Hello '
var b = a
console.log(1, b)
b = ' Modify the value of simple type '
console.log(2, b, a)
// 1 Hello
// 2 Modify the value of simple type Hello
2. Complex data type , assignment , It's a reference address
var a = {
name: ' Hello '
}
var b = a
console.log(1, b)
b.name = ' Modify the value of complex type '
console.log(2, b, a)
// 1 { name: ' Hello ' }
// 2 { name: ' Modify the value of complex type ' } { name: ' Modify the value of complex type ' }
summary
Simply speaking , We use Assignment operator =, When copying values of complex types , The copy is the reference address , operation The copied object will affect the original object .
Shallow copy
But often at work , It is not expected that the operation of the copied data will affect the original data .
At this time, we need to make a copy , New complex data types come out , For our use .
Solution :
- Shallow copy : Shallow copy is an application that copies objects , When the attribute of an object is a reference type , The essence is to copy its reference , When the reference value changes , The copy value also changes .
- Deep copy : Another piece of memory is requested for deep copy , The contents of the copied object are exactly the same as the original , Change the original object , The copy object also changes .
Talk about my own understanding :
Shallow copy : Copied an object , But only the first layer of the copied object , When the attribute of an object is a reference type , This attribute of the copy , It is still the address reference of the copy .
Deep copy : A deep copy copies all the attributes , And copies the dynamically allocated memory that the attribute points to . A deep copy occurs when an object is copied with the object it references .
Common shallow copies
Object.assgin
var obj1 = {
name: ' Start ',
version: 1,
other: {
say: ' Other comments '
}
}
var obj2 = Object.assign({
}, obj1)
console.log(1, obj2)
// 1 { name: ' Start ', version: 1, other: { say: ' Other comments ' } }
obj2.name = ' Modify the first level attribute '
obj2.other.say = ' Modify this object, modify the second level properties '
console.log(2, obj2, obj1)
// 2 { name: ' Modify the first level attribute ', version: 1, other: { say: ' Modify this object, modify the second level properties ' } } { name: ' Start ', version: 1, other: { say: ' Modify this object, modify the second level properties ' } }
/* Shallow copy */
- Expand operator
...
var obj1 = {
name: ' Start ',
version: 1,
other: {
say: ' Other comments '
}
}
var obj2 = {
...obj1 }
console.log(1, obj2)
// 1 { name: ' Start ', version: 1, other: { say: ' Other comments ' } }
obj2.name = ' Modify the first level attribute '
obj2.other.say = ' Modify this object, modify the second level properties '
console.log(2, obj2, obj1)
// 2 { name: ' Modify the first level attribute ', version: 1, other: { say: ' Modify this object, modify the second level properties ' } } { name: ' Start ', version: 1, other: { say: ' Modify this object, modify the second level properties ' } }
/* Shallow copy */
Array.prototype.slice()
var obj1 = [' Start ', {
say: 'old' }]
var obj2 = Array.prototype.slice.call(obj1)
obj2[0] = ' Modify the first level attribute '
console.log(1, obj2)
// 1 [ ' Modify the first level attribute ', { say: 'old' } ]
obj2[1].say = ' Modify this object, modify the second level properties '
console.log(2, obj2, obj1)
// 2 [ ' Modify the first level attribute ', { say: ' Modify this object, modify the second level properties ' } ] [ ' Start ', { say: ' Modify this object, modify the second level properties ' } ]
/* Shallow copy */
边栏推荐
- How about Minsheng futures? Is it safe?
- Some analysis of slow MySQL query
- 必会面试题:1.浅拷贝和深拷贝_浅拷贝
- Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic
- On the problem that the on-board relay cannot be switched on due to insufficient power supply
- Use kettle to read the data in Excel file and store it in MySQL
- 基于FPGA的VGA显示
- 郑慧娟:基于统一大市场的数据资产应用场景与评估方法研究
- Archsummit: evolution of the underlying framework of cherished microservices
- Baidu online disk +chrome plug-in
猜你喜欢

EL & JSTL: JSTL summary

Shardingsphere database sub database sub table introduction

The specified data is grouped and the number of repetitions is obtained in Oracle

Error connecting MySQL database with kettle

Notes of Teacher Li Hongyi's 2020 in-depth learning series 2

Org.json Jsonexception: what about no value for value

JUC concurrent programming - Advanced 05 - lock free of shared model (CAS | atomic integer | atomic reference | atomic array | field updater | atomic accumulator | unsafe class)

认识复杂度和简单排序运算

物联网平台返回数据解析时遇到org.json.JSONException: No value for Value怎么办

Effect evaluation of generative countermeasure network
随机推荐
代码覆盖率
VGA display based on FPGA
Baidu online disk +chrome plug-in
Go基础笔记_4_map
[cloud native] Devops (IV): integrated sonar Qube
一文读懂Elephant Swap的LaaS方案的优势之处
burp从溯源到反制思路
The kettle job implementation runs a kettle conversion task every 6S
The accuracy of float
Read and understand the advantages of the LAAS scheme of elephant swap
Introduction to HLS programming
Notes of Teacher Li Hongyi's 2020 in-depth learning series 3
Flex layout
RichTextBox operation
WPF uses pathgeometry to draw the hour hand and minute hand
Pointrender parsing
Convert a string to an integer and don't double it
JDBC 驱动升级到 Version 8.0.28 连接 MySQL 的踩坑记录
QT6 with vs Code: compiling source code and basic configuration
《元宇宙2086》单日销量超《三体》 夺得京东图书单日科幻榜冠军