1. Shallow cloningShallow cloning cannot copy arrays and objects var obj = { name : "abs", age : '18', sex : 'male' } var obj1 = {} function clone(Origin,target) { target = target || {}; //Prevent users from entering target for(var k in Origin){ target[k] = Origin[k]; } } clone(obj,obj1); 2. Deep cloningFirst determine what it is, a primitive value, an array, or an object, and handle them separately
var obj = { name : 'lin', age : '18', sex : 'male', card : [1,2,3,4], wife : { name : 'bcsds', son : { name : 'aaa' }, age : '23' } } var obj1 = {} //The original value and the object array typeof return value are different function deepClone(origin,target) { target = target || {}; for(var k in origin) { if (origin.hasOwnProperty(k)) { if(typeof(origin[k]) == 'object') { if(Object.prototype.toString.call(origin[k]) == '[object Array]') { target[k] = []; }else { target[k] = {}; } deepClone(origin[k],target[k]); }else { target[k] = origin[k]; } } } } deepClone(obj,obj1); You may also be interested in:
|
<<: Example tutorial on using the sum function in MySQL
>>: Docker network mode and configuration method
This article shares the specific code of js to ac...
1. Create users and authorize Creating users and ...
Dynamically implement a simple secondary menu Whe...
1. Introduction to MMM: MMM stands for Multi-Mast...
background: The site is separated from the front ...
This article example shares the specific code of ...
1. Create tables <br /> First, create two t...
Table of contents 1 What is function currying? 2 ...
1. Use CSS, jQuery, and Canvas to create animatio...
Nginx's configuration syntax is flexible and ...
On a Linux computer, there are two times, one is ...
1. Environmental preparation: Operating system: C...
Table of contents Cache function in vue2 Transfor...
1. Two ways to define react components 1. Functio...
This article example shares the specific code of ...