Some thoughts on memory space caused by JS deep copy

Source: Internet
Author: User
Tags shallow copy

Data Type

JS Common data types are divided into basic types and reference types

    • Basic types: null, undefined, numeric, String, Boolean
    • Reference type: Array, object
Memory space
var a = [1, 2, 3]; var b = a;b[2] = 4; A;   //  ??

We all know that the result is [1, 2, 4], because B and a point to the same reference object so you can change the value of the object, we use memory space to understand it in depth.

We know that there are two areas in memory, one stack stack and the other heap. Usually our basic data types are stored in the stack, and our reference data types exist in the heap. A pointer in the stack points to the data that exists in the heap for easy reference.

var arr = [1, 2, 3];

The memory diagram should look like this:

Arr is a basic type of variable that internally stores an array of addresses/pointers that can be found in the heap object. Usually we say that arr is a reference type and I think it is not rigorous and should say: The variable arr points to a reference type.

A problem arises when you allow two pointers to point to the same heap data, which means that changing the value through one of the pointers affects the other pointer.

var a = [1, 2, 3]; var b = A;

The graph of the above variables in memory is this:

The solution to this problem is to re-create an object that is exactly the same as the reference object in the heap, and then let B point to it, like this: a change of B will not affect a.

A digression: Understanding this mechanism has great benefits for understanding prototype.

function Person (name) {    this. Name == {    sayhi ()} {        Console.log (this . name);}    ; var New Person (' LAN ');p 1.sayHi ();   // ' Hi, Lan '

The memory graph below, you can understand, find out what exists in the stack which exists in the heap:

Next, assign A to B as a shallow copy, while the upper is a deep copy.

JS realizes deep copy idea:

    • The type of the assignment is judged first, and if the primitive type is copied directly, if the object type regenerates an object in the heap, then recursively assigns the value past.
functiondeepcopy (obj) {varresult = '; //as the basic type    if(obj = =NULL|| obj = = Undefined | |typeofObj! = ' object ')        returnobj; //to determine whether an array or an object is a reference type    if(objinstanceofArray) Result= []; Elseresult= {};  for(varKeyinchobj) {        varCurrent =Obj[key]; if(Current = =NULL|| Current = = Undefined | |typeofCurrent! = ' object ') Result[key]=Current ; Else            //do not call deepcopy directly and use Arguments.callee            //error to prevent the function from being assigned to another variableResult[key] =Arguments.callee (current); }     returnresult;}varA ={name:' LAN ', Age:20, Birth: [1,2,3,4], like: {food:' Fruit ', color: [' Pink ', ' blue ']    }};varb =Deepcopy (a); b.birth[2] = 5; A.birth; //[1, 2, 3, 4]

Above for their own a little reflection on the pointer, welcome to correct and expand.

Some thoughts on memory space caused by JS deep copy

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.