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
Table of contents 1. Current situation 2. Create ...
Today, when I was configuring Tomcat to access th...
Read uncommitted example operation process - Read...
When I configured mysql, I set the default storag...
Table of contents What is the reason for the sudd...
Table of contents Implementation ideas: Step 1: C...
By chance, I discovered that a SQL statement prod...
This article shares with you how to use the Vue c...
Table of contents 1. Observable 2. Higher-order f...
Here is a text hovering and jumping effect implem...
Preface Sometimes when hover pseudo-class adds a ...
Table of contents Basic application of javascript...
Table of contents The role of cloneElement Usage ...
Trigger Introduction A trigger is a special store...
1. Introduction to MariaDB and MySQL 1. Introduct...