当前位置:网站首页>用简单方法实现对象的深克隆封装js
用简单方法实现对象的深克隆封装js
2022-06-22 05:24:00 【Best_卡卡】
前言
浅度克隆 :基本类型为值传递,对象仍为引用传递。
深度克隆 :所有元素或属性均完全克隆,并于原引用类型完全独立,
即,在后面修改对象的属性的时候,原对象不会被修改。
简单理解:对象保存在固定位置,浅克隆引用该位置,深克隆是新位置完全复制该对象
实现
// 判断arr是否为一个数组,返回一个bool值
function isArray (arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
}
// 深度克隆
function deepClone (obj) {
if(typeof obj !== "object" && typeof obj !== 'function') {
return obj; //原始类型直接返回
}
var o = isArray(obj) ? [] : {
};
for(i in obj) {
if(obj.hasOwnProperty(i)){
o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
}
}
return o;
}
边栏推荐
- jedispool的使用
- Shenzhen Nanshan District specialized special new small giant enterprise declaration index, with a subsidy of 500000 yuan
- 2022 refrigeration and air conditioning equipment operation test paper and refrigeration and air conditioning equipment operation test skills
- 致远OA漏洞分析、利用与防护合集
- SQL classification, user properties
- 畢業回饋!Apache Doris 社區所有貢獻者來領禮品啦!
- 力扣33【搜索旋转排序数组】
- 毕业季 | 新的开始,不说再见
- Small and medium-sized enterprises should pay attention to these points when signing ERP contracts
- Use keytool to access the JKS file get SSL certificate
猜你喜欢

Does the air valve perform the en 1634-1 fire resistance test for 4 hours?

Liunx virtual machine environment uses docker to install Oracle database and Navicat to connect

Gerrit Code Review Setup

Free questions for polymerization process and test papers for polymerization process in 2022

liunx虚拟机环境使用docker安装oracle数据库,并使用navicat连接
![P1061 [NOIP2006 普及组] Jam 的计数法](/img/53/7ca41b2ed4084f49ebcc2dd47e5919.png)
P1061 [NOIP2006 普及组] Jam 的计数法

JS regular expression to implement the thousands separator
![Force buckle 33 [search rotation sort array]](/img/0f/d7e2f2fdf48bcd70e6c69bca7d4002.png)
Force buckle 33 [search rotation sort array]

Privatized lightweight continuous integration deployment scheme --05- continuous deployment service -drone (Part 1)
![[cloud native] 2.2 kubeadm create cluster](/img/b2/a57b7e44f74357d5aedbb9ddcd95ff.png)
[cloud native] 2.2 kubeadm create cluster
随机推荐
企业如何把ERP项目自动施行?
Rambbmitmq consumer
Privatized lightweight continuous integration deployment scheme --05- continuous deployment service -drone (Part 1)
Traveler's budget (Los Angeles Valley)
北京密云区知识产权贯标的好处,补贴5-10万
C语言数据类型转换规则(隐式转换+显式转换)
SQL classification, user properties
printf becomes puts
Graduation feedback! All contributors of Apache Doris community come to receive gifts!
Tidb performance overview panel
How can enterprises implement ERP projects automatically?
The prediction made ten years ago by the glacier has now been realized by Ali, which is very shocking
1108. Defanging an IP Address
Hanoi Tower problem
Topic selection system of college graduation design based on SSM
通达OA漏洞分析合集
非递归打印斐波那契数列
Remove then add string from variable of Makefile
拉取码云某一个分支的所有数据覆盖本地代码
从JedisSentinelPool获取jedis