当前位置:网站首页>JS written test question -- deep copy of object
JS written test question -- deep copy of object
2022-07-25 02:48:00 【H5_ ljy】
List of articles
One 、 utilize JSON Method
The easiest way , The defect is that there is no copy of the prototype chain , Functions and null No copying
var copy1 = function (obj) {
return JSON.parse(JSON.stringify(obj));
}
var a = {a:function(){console.log('hello world')},b:{c:1},c:[1,2,3],d:"wanger",e:new Date(),f:null,g:undefined}
var b = copy1(a)
console.log(a,b)

Two 、 Using self call of function
Full deep copy
var copy1=function(obj){
if(obj===null){return obj}
if(typeof obj !== 'object'){return obj}
if(obj.constructor==='Date'){return new Date(obj)}
if(obj.constructor==='RegExp'){return new RegExp(obj)}
var newObj=new obj.constructor() // Maintain inheritance chain
for(var key in obj){
if(obj.hasOwnProperty(key)){ // Do not traverse the attributes on its prototype chain
var val=obj[key];
newObj[key]=typeof val ==='object'?arguments.callee(val):val; // Use arguments.callee Decoupling from function names
}
}
return newObj;
}
var a = {a:function(){console.log('hello world')},b:{c:1},c:[1,2,3],d:"ljy",e:new Date(),f:null,g:undefined}
var b = copy1(a);
console.log(a,b);

边栏推荐
- R language one page and many pictures
- Domain driven model (DDD)
- Simulate the implementation of strstr
- Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil
- Physical experiment simulation
- Unity refers to a variable in another class (its own instance)
- Inheritance (prototype)
- Case analysis of building exhibition service management system with low code development platform
- DLL load failed: the page file is too small to complete the operation
- Digital business cloud: how to realize the application value of supplier SRM management system?
猜你喜欢

Tp5.1 include include files (reference public files)

Application method and practical case of sqlmap of penetration test SQL injection

Flume's study notes

Talk about what's going on with C # backstage GC?

Dynamic programming -- Digital DP

Visualization of correlation coefficient matrix

Custom types in C language

6.0 cancellation of member registration verification code
![[C language] program compilation (preprocessing)](/img/94/175a84d89b1f16987e529eb029cbbc.png)
[C language] program compilation (preprocessing)

Permanently mount the image steps
随机推荐
Cloudrev deploy your own public cloud disk (pagoda installation method)
Creating elements of DOM series
After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
Jedispoolconfig parameter configuration from the perspective of source code
It's still a synchronization problem
[circular array] JS method collection of circular array
ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headline
Nuscenes data set summary
Picgo configuring Alibaba cloud OSS
Details of C language compilation preprocessing and comparison of macros and functions
File file name change
"Introduction to interface testing" punch in to learn day05: how can a testing framework support restful interfaces?
String class
Tp5.0 background admin access
Technical experts from large factories: Thoughts on cloud nativity and software supply chain security
Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)
Beginners must see the markdown User Guide
TS uses a third-party library, and there is no type declaration file error handling
YuQue - a useful tool for document writing and knowledge precipitation
MySQL common function summary, very practical, often encountered in interviews