An example of a deep copy of JavaScript--implementation of recursive functions

Source: Internet
Author: User
Tags shallow copy

Today, I learned JS's example of deep copy and shallow copy, some experience.

In JavaScript, there are two copies of a variable, one is a copy of a value, and one is a copy of a memory address, meaning that when assigning to a variable is just a simple basic value, the storage space between the variable and the variable is independent, and the changes between the two variables do not affect each other. When assigning a variable to an object or array, the storage space between the variable and the variable will point to the same memory space, and the value of the memory space will change when a variable is changed.

// Value Copy var i = 4; var j = i; // memory address Copy var m = {name: ' xxx '}; var n = m;

Shallow copy, just a simple variable assignment, if the assigned variable is a reference type of object, the variables will share a memory space, each variable changes will affect the value in the memory space, deep copy, that is, the implementation of the reference type of the data value copied to another newly created storage space.

// Shallow Copy var obj1 = {name = ' xxx '}; var obj2 = obj1; // point to the same memory address // Deep Copy var obj1 = {name = ' xxx '}; var obj2 = {}; // Create a new space obj2.name = Obj1.name; // the value that holds the name

This is a relatively simple copy of the object, and then look at a more complex:

var people = {         name:' xxx ',                                                  // basic value         friends:[' people1 ', ' people2 ', ' Peopple3 '],           // array         info:{                                                           // object               Phone: ' 133xxxxxxxx ' , age               :' + ',               sex:         ' man '}

If this complex object is simply assigned to another variable, it is a shallow copy, so we will encounter some problems:

var man1 = people; var man2 = people;
Man1.name = ' John Doe '; Man2 's name also becomes John Doe man1.friends[0] = ' Zhang San '; // Man2 's first friend also became ' Zhang San 'man1.info.age = ' 20 '; // The age of Man2 has also become

So we need to copy each array and variable. We can encapsulate a function for deep copying:

function (obj,copy) {         for (var in obj) {               = obj[i];   Copy each item in obj          }         return  copy;

The problem with this writing is that when an attribute in obj is still a variable of reference type, it points to the same memory space. So we can use the recursive method, let is the object's property and then through the historical assignment.

functiondeepcopy (obj,copy) { for(varIinchobj) {                     if(typeOf obj[i] = = ' object ') {//determines whether an objectDeepcopy (Obj[i],copy[i]);//callback deep copy function                     }                     Else{Copy[i]= Obj[i];//not object Direct copy                     }           }             returncopy;} 

This will make it possible to copy all the data into another storage space.

An example of a deep copy of JavaScript--implementation of recursive functions

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.