当前位置:网站首页>Implementation of deep copy deepclone
Implementation of deep copy deepclone
2022-07-23 17:31:00 【No overtime at the front end】
encapsulation
export function deepclone (obj) {
var o
// If He is the object object Words , because null,object,array It's also 'object';
if (typeof obj === 'object') {
// If If he is empty
if (obj === null) {
o = null
} else {
// If He's an array arr Words
if (obj instanceof Array) {
o = []
for (var i = 0, len = obj.length; i < len; i++) {
o.push(deepclone(obj[ i ]))
}
}
// If He is the object object Words
else {
o = {
}
for (var j in obj) {
if (j != 'root' && j != 'parent') {
o[ j ] = deepclone(obj[ j ])
}
}
}
}
} else {
o = obj
}
return o
};
1、 Deep copy : The result changes after copying will not affect the results before copying It doesn't matter before or after copying
2、 Shallow copy : Change the content before copying , It will affect the copy There is a relationship between pre copy and post copy
// The problem of citation relationship
// ... Operators can only copy one layer
// ... The first layer is deep copy , The second time is shallow copy .
let obj={
name:'jim',address:{
x:100,y:100}};
let o={
...obj};
obj.name='hello';
console.log(obj,o);
let a=[1,2,4];
let arr=[a];
let newArr=arr.slice(); // Shallow copy
newArr[0][0]=100;
console.log(arr);
3、 Deep copy JSON.parse(JSON.stringify(obj)); Incomplete Complex copies cannot be achieved
let obj={
name:'jim',address:{
x:100,y:100},f:function(){
}};
let o=JSON.parse(JSON.stringify(obj));
obj.address.x=200;
console.log(obj,o);
4、 Implement a deep copy Implement a recursive copy
function deepClone(obj,hash=new WeakMap()){
if(obj==null) return obj; // If it is null perhaps undefined I don't copy
if(obj instanceof Date) return new Date(obj);
if(obj instanceof RegExp) return new RegExp(obj);
// It could be an object Or ordinary value If it's a function , There is no need for deep copy Because functions are used to call , There is no need to copy a new function
if(typeof obj!=='object') return obj;
// If it's an object, it's a deep copy [] {} Object.prototype.toString.call(obj)==[object Array]?[]:{}
if(hash.get(obj)) return hash.get(obj);
let cloneObj=new obj.constructor;
hash.set(obj,cloneObj);
for(let key in obj){
if(obj.hasOwnProperty(key)){
// Implement a recursive copy
cloneObj[key]=deepClone(obj[key],hash);
}
}
return cloneObj;
}
let obj={
name:1,address:{
x:1000}}
let d=deepClone(obj);
obj.address.x=200;
console.log(d);
// If the object is more complex Circular reference
边栏推荐
- 阿里二面:什么是CAS?
- Unity production QR code scanning
- @Bean 注解的方法调用多次会创建多个bean 实例吗
- [mysql] I. MySQL starts
- OpenCV求两个区域的交集
- Keil errors and solutions (1): fcarm - output name not specified, please check 'options for target - Utilities‘
- 单细胞论文记录(part19)--A comprehensive comparison on cell-type composition inference for ST data
- Redis分布式锁,没它真不行
- Preliminary understanding of string
- 食品安全|巧克力也有真假?关于它你了解多少
猜你喜欢

General paging implementation

别再问我MySQL为啥没走索引?就这几种原因,全都告诉你

面试官:MySQL 数据库查询慢,除了索引问题还可能是什么原因?

59. General knowledge of lightning safety

Food safety chocolate is also true or false? How much do you know about it

Kubernetes kubelet manages pod core process

xlinx pcie xvc

Detailed explanation of SQL bool blind note and time blind note

【flask高级】从源码深入理解flask路由之endpoint

日志瘦身骚操作:从5G优化到1G!
随机推荐
Explain SQL optimization in detail
Detailed explanation of SQL error reporting and blind annotation
Pymoo learning (3): use multi-objective optimization to find the set of optimal solutions
【Js】检查Date对象是否为Invalid Date
Typescript 清空数组
Ros2 self study notes: rqt visualization tool
LQR 控制学习-LQR控制 MATLAB官方教程-LQR 控制器_状态空间系统Matlab/Simulink建模分析
IR drop, EM, noise and antenna
Food safety chocolate is also true or false? How much do you know about it
几何参数化重构
单细胞文献学习(part6)--ForestFireClustering for sc sequencing combines iterative label propagation with ...
Software configuration | pychart download, installation, environment configuration and uninstall
可视化机房管理
Pymoo learning (2): Bi objective optimization problems with constraints
Preliminary tutorial of Hezhou esp32c3 PIO Arduino development framework based on vscode
unity之制作二维码扫描
Vscode - code and file changes cannot be saved
[flask advanced] deeply understand the endpoint of flask routing from the source code
面试官:MySQL 数据库查询慢,除了索引问题还可能是什么原因?
59. General knowledge of lightning safety