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
need: Implement dynamic display of option values ...
Recently, I need to do a small verification exper...
1. Download the installation script - composer-se...
Recently, when I was using the Linux operating sy...
Mainly from two aspects: 1. Highlight/Line Break ...
mysql dirty pages Due to the WAL mechanism, when ...
Table of contents 1. Overview 2. Application Exam...
Table of contents 1. The role of watch in vue is ...
1. Division of labor and process <br />At T...
Table of contents 1. How are structures stored in...
I have been having this problem recently when desi...
<br />Related articles: 9 practical suggesti...
Web design, according to personal preferences and ...
Vue2+elementui's hover prompts are divided in...
step Place the prepared static resource files in ...