On deep cloning and shallow cloning in JS

Source: Internet
Author: User

In JS, we usually create a JSON object with VAR to store the data conveniently.

var template = {

User: ' Zhang ',

Password: ",

tem:[' label 1 ', ' label 2 ']

}

This approach is generally used as a standard data format.

Let's start with a shallow clone.

Then we look at the following code

function SetObject (obj) {
var newObj = {};
for (var i in obj) {
Newobj[i] = Obj[i]
}
return NEWOBJ;
}

var tem2 = setobject (template);
Tem2.user= "Shaw";
Tem2.password = "123";
Tem2.tem.push (' music ');

var tem3 = setobject (template);
Tem3.user= "Liu MoU";
Tem3.password = "123456";
Tem3.tem.push (' Sports ');

Console.log (TEM2)//

    1. "Label 1"
    2. 1:"label 2"
    3. 2:"Music "
    4. 3:"Sport "


Console.log (TEM3)//

    1. "Label 1"
    2. 1:"label 2"
    3. 2:"Music "
    4. 3:"Sport "

The tem2 tem is seen by printing as an array of tem3 tem.

This is very well understood, because a simple copy of the object, if one of the objects is a reference variable, this happens, because the reference variable is stored in memory address, so in fact, the operation is the same piece of memory, resulting in the same array content.

Let's take a look at the following situation

function SetObject (obj,newobj) {
var newObj = NEWOBJ | | {};
for (var i in obj) {
if (typeof obj[i] = = ' object ') {
Newobj[i] = (Obj[i].constructor = = = = Array)? [] : {}
SetObject (Obj[i],newobj[i])
}else{
Newobj[i] = Obj[i]
}
}
return NEWOBJ;
}

var tem2 = setobject (template);
Tem2.user= "Shaw";
Tem2.password = "123";
Tem2.tem.push (' music ');


var tem3 = setobject (template);
Tem3.user= "Liu MoU";
Tem3.password = "123456";
Tem3.tem.push (' Sports ');

Console.log (TEM2)//tem2.tem

Array (3)

      1. 0:" Label 1 "
      2. 1< Span class= "Object-properties-section-separator" >:" label 2 "
      3. 2:" music "
      4. length:3


Console.log (TEM3)//Tem3.tem

    1. Array (3)
    2. 0:"label 1"
    3. 1:"label 2"
    4. 2:"Sport "
    5. Length:3

Deep cloning is in the cloning of the time to determine whether the type of the property is a reference variable, if it is a recursive method to let it in a layer to copy themselves.

This is the depth of cloning, in fact, JS is a method of inheritance.

On deep cloning and shallow cloning in JS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.