当前位置:网站首页>(JS)手写深拷贝
(JS)手写深拷贝
2022-06-29 09:57:00 【不愿透露姓名的余菜鸟】
JS手写深拷贝
const obj1 = {
age: 20,
name: "xxx",
address: {
city: "earth"
}
};
/** * * @param obj {Object} */
function deepClone(obj = {
}) {
//如果obj不是对象或者为空,则直接返回一个空对象
if (typeof obj !== "object" || obj == null) {
return obj;
}
let result;
//如果obj是对象,则判断他是否为数组,如果是数组,则为result赋值为数组
if (obj instanceof Array) {
result = [];
} else {
result = {
};
}
//遍历obj的属性
for(let key in obj) {
//判断是否为obj自身的属性,如果是则使用递归赋值给result,也可以直接使用obj[key]赋值,结果不变
if(obj.hasOwnProperty(key)) {
result[key] = deepClone(obj[key]);
}
}
return result;
}
console.log(deepClone(obj1))
边栏推荐
- Real test = "half product + Half development"?
- MySQL查询时如何找出错误格式的手机号
- Highly paid programmers & interview questions: how to ensure the data consistency between redis cache and database in series 117?
- Here comes the tutorial of datawhale recommendation system!
- Does your project need automated testing?
- Please tell me about the Flink SQL batch task, two or more tables join (inner join or outer join
- Dormitory maintenance management system based on stm32+rfid design
- FreeRTOS porting of official website based on keil5 auto configuring STM32F103 standard library
- 在 2022 年找工作,毕业生们的 “最后一课”
- Given the values of two integer variables, the contents of the two values are exchanged (C language)
猜你喜欢

30岁,女,普通软件测试媛,对职业的迷茫和焦虑

真正的测试 =“半个产品+半个开发”?

Fully understand the MESI cache consistency protocol

他98年的,我玩不过他...

Buuctf-- connotative software

Comprehensive understanding of synchronized

有了这款工具,自动化识别验证码再也不是问题

QT writing IOT management platform 37- logic design

Print prime numbers between 100 and 200 (C language)

ssh密钥泄露(B模块赛题)——应用服务漏洞扫描与利用
随机推荐
WinForm uses zxing to generate QR code
Does anyone encounter this problem when flinkcdc synchronizes MySQL?
全面理解MESI缓存一致性协议
高效工作必备:测试人如何提高沟通技能?
Bug description, rating and life cycle
认不出原来的模样
SQL Server 数据库的几种简单查询
dropout层
【C语言进阶】自定义类型
“AI x 科学计算”进行时,华为昇思 MindSpore 赛题火热开启,等你来!
X-Frame-Options(点击劫持) 网页劫持漏洞
Dropout layer
Reprint: five methods to determine whether an object has attributes
Linux下Redis安装及集群搭建
What happened during the MySQL installation?
当技术人成长为 CEO,应该修改哪些“Bug”?
Analysis of reentrantlock source code of AQS
反CSRF爆破的三种姿势
《如何阅读一本书》读后总结
By asp Net core downloading files according to the path