About JS Stacks and copies

Source: Internet
Author: User

1. Stacks (Stack) and the heaps (heap)

  The stack is automatically allocated memory space, which is automatically freed by the system, while the heap is dynamically allocated memory, and the size is not automatically released.

2. Basic types and reference types

  Basic type: a simple data segment stored in the stack memory, the size of the data is determined, and the size of the memory space can be allocated.

5 basic data types are Undefined, Null, Boolean, number, and String, which are stored directly by value, so they can be accessed directly.

  Reference type: an object stored in heap memory, where the variable actually holds a pointer to another location. Each space size is different, depending on the circumstances of the specific allocation.

When we need to access the value of a reference type (such as an object, an array, a function , etc.), we first get the object's address pointer from the stack, and then get the required data from the heap memory.

3. Transmit Value and address

The previous reason for explaining what is in-memory heap, stack, and variable types is actually served below to better understand what "shallow copy" and "Deep copy" are.

The difference between the basic type and the reference type is actually the difference between the value of the transmission and the address . Test Case:

1     var a = [1,2,3,4,5]; 2     var b = A; 3     var c = a[0]; 4     alert (b);//1,2,3,4,5 5     alert (c);//1 6     //Change value         7     B[4] = 6; 8     C = 7; 9     alert (a[4]);//610     alert (a[0]);//1

From the above we can tell that when I change the data in B, the data in a is changed, but when I change the data value of C, a does not change.

This is the difference between the value of the transmission and the address . Because A is an array , it is a reference type , so it is given to B by the address in the stack (the equivalent of creating a new "pointer" without the same name), not the object in the heap memory. and c is just a data value obtained from a heap of memory and stored in the stack. So when B is modified, it is changed from the address to the a heap, and C is modified directly in the stack and cannot point to a heap of memory.

3. Shallow copy

As mentioned earlier, when defining an object or an array, the variable is often stored as an address. When we use the object copy, if the property is an object or an array , then we are passing only one address. As a result, when the child object accesses the property, it is traced back to the heap memory pointed to by the parent object, that is, the parent- child object is associated , and the property values point to the same memory space.

1 var a = {2         key1: "11111" 3     } 4     function Copy (p) {5         var c = {}; 6 for         (var i in P) {  7         C [i] = p[i]; 8         } 9         return c;10}11     a.key2 = [' Small Fai ', ' FAI '];12     var b = Copy (a); B.key3 = ' 33333 ';     alert (b.ke y1);     111111115     alert (b.key3);    3333316     alert (a.key3);    Undefined

The Key1 property in a object is a string, and the Key2 property is an array. A copy to b,12 property is copied smoothly. When a new property of type Key3 is added to a B object, B is modified normally, and no is defined in a. Indicates that the Key3 (base type) of the child object is not associated with the parent object, so undefined.

1 B.key2.push ("Da Hui"); 2 alert (b.key2);    Xiao Hui, Xiao Hui, da Hui 3 alert (a.key2);    Xiao Hui, Xiao Hui, da Hui

However, if the modified property becomes an object or an array, an association occurs between the parent and child objects. From the above results, I have modified the B object, the Key2 property value (array) of A and B has changed. It is in the state of memory that can be used to represent.

The reason is that the value of Key1 is the basic type, so the data is passed when the copy is, but the value of Key2 is the object in the heap memory, so Key2 passes the address to the Key2 object at the time of copying, no matter how many Key2 are copied, Its value is always the memory space of the Key2 object that points to the parent object.

Below is an example of a $.extend () shallow copy.

varObject1 ={apple:0, Banana: {weight: the, Price: -}, Cherry: the};varObject2 ={banana: {price: $}, Durian: -};//Shallow Copy by default
$.extend (son, Father)
//$.extend (b,a)//$.extend (Object1, object2);//Copy Object2 to Object1, Object2 's Banana property passes the address of the banana object at the time of the copy, so in Object1, Banana points to the banana in object2.

Object1--->{"apple": 0, "banana": {"Price": $, "cherry": $, "Durian": 100}

4. Deep copy

Perhaps the above is not the result we want in the actual coding, we do not want to have the parent-child association between the object , then this time can use a deep copy . Since the property value type is an array and or as if it only addresses, then we use recursion to solve this problem, the parent object in all of the object's property types are traversed assigned to the child object. The test code is as follows:

1 function Copy (A1, B1) {2         var B1 = B1 | | {}; 3 for         (var i in A1) {4         if (typeof a1[i] = = = ' object ') {5         b1[i] = (A1[i].constructor = = = Array)? [] : {}; 6         Copy (A1[i], b1[i]); 7         } else {8         b1[i] = a1[i]; 9         }10         }11         return b1;12}    13     A.key2 = [' Small Fai ', '];14     var b={};15     b = Copy (A, b);     B.key2.push ("Big Fai");     alert (b.key2);    Xiao Hui, Xiao Hui, da Hui-     alert (a.key2);    Xiao Hui, Xiao Hui

From the above, when modifying the key2 array of B, there is no new value for the Key2 array in the A parent object, that is, the child object does not affect the Key2 in parent object A. The storage mode is roughly as follows:

The following is an example of a $.extend () deep copy.

varObject1 ={apple:0, Banana: {weight: the, Price: -}, Cherry: the};varObject2 ={banana: {price: $}, Durian: -};//Deep Copy//object1--->{"apple": 0, "banana": {"Weight" : "Price": $, "cherry": $, "Durian": $//Object2 's Banner property belongs to the object type, recursively assigns the object type traversal of the property to the child object, and the banner of the sub-object Object1 is no longer the memory space of the Object2 property that points to the banana parent object. $.extend (true, Object1, object2);
Object1--->{"apple": 0, "banana": {"Weight": "Price": $, "cherry": $, "Durian": 100}
console.log ('object1--->'+json.stringify (Object1));

Transferred from: http://www.cnblogs.com/chengguanhui/p/4737413.html

About JS Stacks and copies

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.