当前位置:网站首页>必会面试题:1.浅拷贝和深拷贝_深拷贝
必会面试题:1.浅拷贝和深拷贝_深拷贝
2022-07-24 22:51:00 【lazytomato】
深拷贝
start
- 新建一个内存区域,拷贝元数据,
1.最常见的
最常见的深拷贝 那肯定就是 JSON.stringify(JSON.parse())
但是它有缺陷:
- 不支持 undefined,function,Symbol
- 不支持循环引用
- 不支持某些特殊的对象类型
- 等
它满足大多数的使用场景,也算是一个还不错的方法。
手写一个深拷贝
function deepClone (target, hash = new WeakMap()) {
// 额外开辟一个存储空间WeakMap来存储当前对象
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 // 处理 DOM元素
if (typeof target !== 'object') return target
if (hash.get(target)) return hash.get(target) // 当需要拷贝当前对象时,先去存储空间中找,如果有的话直接返回
const cloneTarget = new target.constructor()
hash.set(target, cloneTarget) // 如果存储空间中没有就存进 hash 里
Reflect.ownKeys(target).forEach(key => {
cloneTarget[key] = deepClone(target[key], hash) // 递归拷贝每一层
})
return cloneTarget
}
注意事项:
- 上述代码为ES6版本的,暂考虑常见的情况了。
- 引入了
WeakMap new target.constructor()传入参数是什么类型就,声明一个什么类型的数据(区分数组和对象)Reflect.ownKeys(Reflect.ownKeys方法返回一个由目标对象自身的属性键组成的数组。它的返回值等同于)他可以拷贝Symbol类型的键
其他问题
1.循环引用
通过WeakMap 或者一个数组存储已深克隆的数据,每次克隆之前对比一下即可。已克隆直接return
2.需要拷贝的对象,多个属性指向同一个引用地址,(也叫引用丢失)
解决方式同问题一,直接数组存储已克隆的数据,重复数据直接return即可。
3.递归暴栈
当层级很多的时候,递归调用,很可能会导致递归暴栈的情况,这种方式解决方法,可以参考文章,不适用递归,而是使用循环去做这个地方。
优质博客
- https://juejin.cn/post/7072528644739956773#heading-0
- https://juejin.cn/post/6844903775283445767#heading-5
- https://segmentfault.com/a/1190000016672263
边栏推荐
- Okaleido tiger NFT即将登录Binance NFT平台,后市持续看好
- ODBC executes stored procedure to get return value
- Website resources
- 基于FPGA的VGA显示
- Joint search set structure
- Background image and QR code synthesis
- The tragic experience of installing scikitlearn on win764
- [cloud native kubernetes] kubernetes cluster advanced resource object staterulesets
- When texturebrush is created, it prompts that there is insufficient memory
- The size of STM32 stack
猜你喜欢

Time series data in industrial Internet of things

Glidemodule appglidemodule and generated API details

Archsummit: evolution of the underlying framework of cherished microservices

EL & JSTL: JSTL summary

百度网盘+Chrom插件

Error connecting MySQL database with kettle

力扣 1184. 公交站间的距离

Read and understand the advantages of the LAAS scheme of elephant swap

burp从溯源到反制思路

Multi task face attribute analysis based on deep learning (based on paddlepaddle)
随机推荐
[cloud native] Devops (IV): integrated sonar Qube
CSF cloth simulation filtering for PCL point cloud processing (59)
About constant modifier const
[zero basis] SQL injection for PHP code audit
Old Du servlet JSP
理财产品可以达到百分之6的,我想要开户买理财产品
"Fundamentals of program design" Chapter 10 function and program structure 7-2 Hanoi Tower problem (20 points)
Notes of Teacher Li Hongyi's 2020 in-depth learning series lecture 1
PGPLSQL中的:=和=
用VS Code搞Qt6:编译源代码与基本配置
Multi task face attribute analysis based on deep learning (based on paddlepaddle)
GB、MB、KB分别是什么意思。大小分别是多少
把字符串转换成整数与不要二
痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
QT6 with vs Code: compiling source code and basic configuration
Talk about how redis handles requests
QT6 with vs Code: compiling source code and basic configuration
物联网平台返回数据解析时遇到org.json.JSONException: No value for Value怎么办
国信证券手机开户安全吗
Violent recursion - detailed explanation of Queen n & how to optimize with bit operation