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

The front end creates and modifies CAD graphics details through JavaScript

Table of contents 1. Current situation 2. Create ...

MySQL detailed explanation of isolation level operation process (cmd)

Read uncommitted example operation process - Read...

Setting the engine MyISAM/InnoDB when creating a data table in MySQL

When I configured mysql, I set the default storag...

How to achieve the maximum number of connections in mysql

Table of contents What is the reason for the sudd...

Complete step record of vue encapsulation TabBar component

Table of contents Implementation ideas: Step 1: C...

Solution to the problem that order by is not effective in MySQL subquery

By chance, I discovered that a SQL statement prod...

Detailed explanation of using Vue custom tree control

This article shares with you how to use the Vue c...

A brief talk about Rx responsive programming

Table of contents 1. Observable 2. Higher-order f...

CSS 3.0 text hover jump special effects code

Here is a text hovering and jumping effect implem...

How to solve the element movement caused by hover-generated border

Preface Sometimes when hover pseudo-class adds a ...

Basic application methods of javascript embedded and external links

Table of contents Basic application of javascript...

Detailed explanation of the use of React.cloneElement

Table of contents The role of cloneElement Usage ...

Introduction and use of triggers and cursors in MySQL

Trigger Introduction A trigger is a special store...

Detailed steps for smooth transition from MySQL to MariaDB

1. Introduction to MariaDB and MySQL 1. Introduct...