A brief talk about cloning JavaScript

A brief talk about cloning JavaScript

1. Shallow cloning

Shallow 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 cloning

First determine what it is, a primitive value, an array, or an object, and handle them separately

  • Iterating over objects
  • The original value is copy directly
  • Not a primitive value, judge whether it is an array or an object
  • Is an array to create an empty array
  • It is an object that creates an empty object
  • After it is established, iterate over what is in the original object or array
  • recursion
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:
  • Detailed explanation of the deep and shallow cloning principles of JavaScript arrays and non-array objects
  • JavaScript Shallow Cloning and Deep Cloning Examples Detailed
  • JavaScript shallow cloning, deep cloning comparison and example analysis
  • Example of deep cloning method for JS objects
  • Analysis of JS object deep cloning examples
  • JS extension class, cloned object and mixed class instance analysis
  • JS clone, attribute, array, object, function instance analysis
  • JavaScript deep clone object detailed explanation and example
  • Simple implementation method of javascript array cloning
  • Introduction to common methods of cloning objects and arrays in js

<<:  Example tutorial on using the sum function in MySQL

>>:  Docker network mode and configuration method

Recommend

Vue + element to dynamically display background data to options

need: Implement dynamic display of option values ...

Steps to install MySQL on Windows using a compressed archive file

Recently, I need to do a small verification exper...

How to install Composer in Linux

1. Download the installation script - composer-se...

Analysis of the principles of Mysql dirty page flush and shrinking table space

mysql dirty pages Due to the WAL mechanism, when ...

Summary of fragmented knowledge of Docker management

Table of contents 1. Overview 2. Application Exam...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Tudou.com front-end overview

1. Division of labor and process <br />At T...

Detailed explanation of Linux kernel macro Container_Of

Table of contents 1. How are structures stored in...

Tips on HTML formatting and long files for web design

<br />Related articles: 9 practical suggesti...

Some issues we should pay attention to when designing a web page

Web design, according to personal preferences and ...

Use vue2+elementui for hover prompts

Vue2+elementui's hover prompts are divided in...

How to publish static resources in nginx

step Place the prepared static resource files in ...