当前位置:网站首页>Mandatory interview questions: 1. shallow copy and deep copy_ Deep copy
Mandatory interview questions: 1. shallow copy and deep copy_ Deep copy
2022-07-26 02:18:00 【lazytomato】
Deep copy
start
- Create a new memory area , Copy metadata ,
1. The most common
The most common deep copy That must be JSON.stringify(JSON.parse())
But it has flaws :
- I won't support it undefined,function,Symbol
- Circular references... Are not supported
- Some special object types are not supported
- etc.
It meets most usage scenarios , It is also a good method .
Write a deep copy
function deepClone (target, hash = new WeakMap()) {
// Open up an extra storage space WeakMap To store the current object
if (target === null) return target
if (target instanceof Date) return new Date(target)
if (target instanceof RegExp) return new RegExp(target)
if (target instanceof HTMLElement) return target // Handle DOM Elements
if (typeof target !== 'object') return target
if (hash.get(target)) return hash.get(target) // When you need to copy the current object , First go to the storage space to find , If there is one, go straight back to
const cloneTarget = new target.constructor()
hash.set(target, cloneTarget) // If not in the storage space, save hash in
Reflect.ownKeys(target).forEach(key => {
cloneTarget[key] = deepClone(target[key], hash) // Recursively copy each layer
})
return cloneTarget
}
matters needing attention :
- The above code is ES6 Version of , Consider the common situation for the time being .
- Introduced
WeakMap new target.constructor()The type of the incoming parameter is , Declare what type of data ( Distinguish between arrays and objects )Reflect.ownKeys(Reflect.ownKeysMethod returns an array of the property keys of the target object itself . Its return value is equal to ) He can copy Symbol Type key
Other questions
1. Circular reference
adopt WeakMap Or an array to store deeply cloned data , Just compare it before each clone . Cloned direct return
2. Objects that need to be copied , Multiple attributes point to the same reference address ,( Also known as reference loss )
The solution is the same as the problem , Direct array stores cloned data , Duplicate data directly return that will do .
3. Recursive stack explosion
When there are many levels , Recursively call , It is likely to lead to recursive stack explosion , The solution in this way , You can refer to article , Not applicable to recursion , Instead, use a loop to do this place .
Quality blog
- https://juejin.cn/post/7072528644739956773#heading-0
- https://juejin.cn/post/6844903775283445767#heading-5
- https://segmentfault.com/a/1190000016672263
边栏推荐
- Prometheus+blackbox exporter+grafana monitoring server port and URL address
- Advantages of composition API
- Pytorch的API总览
- Error reporting caused by local warehouse
- I.MX6UL核心模块使用连载-nand flash读写测试 (三)
- Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
- Illustration of the insertion process of b+ tree
- TCP three handshakes and four waves
- numpy.sort
- 数仓:浅谈银行业的数仓构建实践
猜你喜欢

【云原生】4.1 DevOps基础与实战

Illustration of the insertion process of b+ tree
![[cloud native] 4.1 Devops foundation and Practice](/img/09/5423540d0a4a11bc7162c5ab343a4d.png)
[cloud native] 4.1 Devops foundation and Practice

租户问题。

I.MX6UL核心模块使用连载-eMMC读写测试 (四)

力扣148:排序链表

Prometheus + redis exporter + grafana monitor redis service

Worthington nuclease and Micrococcus related research and determination scheme

1. Mx6ul core module use serialization - view system information (II)

Prometheus + process exporter + grafana monitor the resource usage of the process
随机推荐
Web3.0 blog DAPP development practice [2022]
I.MX6UL核心模块使用连载-查看系统信息 (二)
LeetCode_ Dynamic programming_ Medium_ 264. Ugly number II
2022.7.25-----leetcode.919
Remember a laravel problem script @php artist package:discover handling the post autoload dump event returned with
A MCU event driven C framework
【2021】【论文笔记】6G技术愿景——OTFS调制技术
Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)
(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
MySQL transaction isolation level
1205 Lock wait timeout exceeded; Try restarting transaction processing
2022.7.25-----leetcode.919
Ti AM335X工控模块使用beaglebone(bbb)的Debian系统
18_ Request file
一款可插拔的AM335X工控模块板载wifi模块
A pluggable am335x industrial control module onboard WiFi module
Implementation of Ti am335x industrial control module network and file system nfs
I.MX6UL核心模块使用连载-触摸屏校准 (九)
Be careful about bitmap, the "memory Assassin"~
我来图书馆小程序一键签到和一键抢位置工具