当前位置:网站首页>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);

边栏推荐
- Actual combat in ThreadLocal project
- How to use blender to make 360 degree panorama and panoramic video?
- Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt
- Digital business cloud: how to realize the application value of supplier SRM management system?
- Remote sensing image classification tool and visualization application of WebGIS
- Simulate the implementation of StrCmp function
- File file name change
- Simulate the implementation of strstr
- Explorer TSSD 2019 software installation package download and installation tutorial
- Jenkins configuration plug-in interface displays "suggestions collection" in Chinese
猜你喜欢

Is redis'module'not an internal or external command?

Generator set work arrangement problem code

C: wechat chat software instance (wpf+websocket+webapi+entityframework)

Digital commerce cloud fine chemical industry management platform integrated informatization solution

2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.

Explanation of C language file operation function

On Calc optimization of calcite

Tp5.1 include include files (reference public files)

Tp5.1 paging (with parameter transfer)

Visualization of correlation coefficient matrix
随机推荐
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
"Introduction to interface testing" punch in to learn day09: Micro service interface: how to use mock to solve chaotic call relationships
Domain driven model (DDD)
A bit of knowledge - websites about scripts
Tp5.1 login configuration method of whether to login public functions (complete login case)
Matlab for circular pit
Cloudrev deploy your own public cloud disk (pagoda installation method)
Permanently mount the image steps
Simulate the implementation of StrCmp function
Jedispoolconfig parameter configuration from the perspective of source code
[system design] distributed key value database
Automatic backup of Linux server PostgreSQL database
Beginners must see the markdown User Guide
Matlab draws radar chart (four lines of code)
Keil compile download error: no algorithm found for: 08000000h - 08001233h solution
MySQL common function summary, very practical, often encountered in interviews
Common Oracle commands
Study notes of filebeat
The file in scanpy1.9.1 cannot be read in scanpy1.7.2. The problem is solved
Analysis of FLV packaging