当前位置:网站首页>(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))
边栏推荐
- The encryption market has exploded one after another. Can Celsius avoid bankruptcy?
- Dormitory maintenance management system based on stm32+rfid design
- JS post download file
- 真正的测试 =“半个产品+半个开发”?
- Excel date and number format processing
- Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
- 小米手机-解BL锁+开ROOT权限
- 加密市场接连爆雷,Celsius能避免破产吗?
- C language printf family
- Does anyone encounter this problem when flinkcdc synchronizes MySQL?
猜你喜欢
随机推荐
基于STM32+RFID设计的宿舍检修管理系统
BUUCTF--reverse2
The product strength is not inferior to that of BYD. Geely Dihao l Raytheon hi · x delivered 10000 units in the first month
《如何阅读一本书》读后总结
励志!专科“逆袭”读浙大硕士,3篇SCI,最终成清华博士!
[FreeRTOS] 08 mutex semaphores and priority inversion
Findbugs修改总结
UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. 笔记
Here comes the tutorial of datawhale recommendation system!
Mongodb tutorial Chapter 02 mongodb installation
BUUCTF RE-easyre
在 2022 年找工作,毕业生们的 “最后一课”
Comprehensive understanding of synchronized
Print leap years between 1000 and 2000 (C language)
With this tool, automatic identification and verification code is no longer a problem
学习通否认 QQ 号被盗与其有关:已报案;iPhone 14 量产工作就绪:四款齐发;简洁优雅的软件早已是明日黄花|极客头条...
罗清启:高端家电已成红海?卡萨帝率先破局
AQS之Atomic详解
LVGL库入门教程 - 动画
Ora-01950 does not have permission on tablespace




![[FreeRTOS] 08 mutex semaphores and priority inversion](/img/16/9715d5599db6ec107c8001fbd70ae2.png)



