当前位置:网站首页>js深拷贝-笔记
js深拷贝-笔记
2022-07-29 05:09:00 【青山绿水的蓝】
递归实现
适用任何数据类型
//实现深克隆
let ooo={name:'name',age:'age',sex:[['sex']],ok:()=>console.log(1)}
const deepClone=(obj)=>{
//判断是否为Object,Array的类型
if(obj&&typeof obj==='object'){
//引用类型
//判断是[]还是{}
let temp=Array.isArray(obj)?[]:{}
//循环操作,递归
for(let i in obj){
temp[i]=deepClone(obj[i])
}
//返回temp
return temp
}else{
//除Object,Array以外的类型直接返回obj
return obj
}
}
let obj=deepClone(ooo)
obj.gender='man'
console.log(obj,ooo)
JSON
会导致数据丢失(如函数)
let obj={name:'小王八',msg:{age:18,gender:'man'}}
let newObj=JSON.parse(JSON.stringify(obj))
newObj.msg.gender='男'
console.log(obj)
边栏推荐
- In depth analysis of common cross end technology stacks of app
- 直播预告|如何通过“智能边缘安全”提升企业免疫力?
- 51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
- 省市区三级联动(简单又完美)
- QT series - Installation
- 递归的基本原理
- 为啥谷歌的内部工具不适合你?
- During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
- Best practices of JD cloud Distributed Link Tracking in financial scenarios
- vim编辑器使用
猜你喜欢

Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com

C language first level pointer

365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13

京东云金秋上云特惠进行中!扫码参与活动

串口通讯部分详解

CMU15-213 Malloc Lab实验记录

科班同学真的了解未来的职业规划吗?

The road to success in R & D efficiency of 1000 person Internet companies

【活动预告】云上数字工厂与中小企业数字化转型创新论坛

QML control: combobox
随机推荐
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest
Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
一维数组练习
AD常用快捷键
抢先预约 | 阿里云无影云应用线上发布会预约开启
题解:在一个排序数组中查找元素第一个和最后一个的位置 (个人笔记)
携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
ANSI C类型限定符
C语言数组典型应用代码详细讲解—高手误入(逐步代码详解)
AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
阿里云联合鼎捷软件发布云上数字工厂解决方案,实现云MES系统本地化部署
JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
【活动预告】云上数字工厂与中小企业数字化转型创新论坛
JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
Occt learning 003 - MFC single document project
With frequent data leakage and deletion events, how should enterprises build a security defense line?