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

js realizes 3D sound effects through audioContext

This article shares the specific code of js to ac...

CSS to achieve dynamic secondary menu

Dynamically implement a simple secondary menu Whe...

MySQL high availability solution MMM (MySQL multi-master replication manager)

1. Introduction to MMM: MMM stands for Multi-Mast...

Nginx external network access intranet site configuration operation

background: The site is separated from the front ...

Vue implements verification whether the username is available

This article example shares the specific code of ...

JavaScript Function Currying

Table of contents 1 What is function currying? 2 ...

Nginx try_files directive usage examples

Nginx's configuration syntax is flexible and ...

How to modify the time zone and time in Ubuntu system

On a Linux computer, there are two times, one is ...

Centos7.5 installs mysql5.7.24 binary package deployment

1. Environmental preparation: Operating system: C...

How to use Vue cache function

Table of contents Cache function in vue2 Transfor...

React Class component life cycle and execution order

1. Two ways to define react components 1. Functio...

Vue implements carousel animation

This article example shares the specific code of ...