JS Stack and copy
SOURCE http://www.cnblogs.com/jingwhale/p/4884759.html
I. Definition of a stack
1. The stack is a special linear table. The peculiarity is that the manipulation of inserting and deleting data elements can only be done at one end of the linear table.
Conclusion: LIFO (last in First out), abbreviated to LIFO linear table.
The application of stack is: number conversion, grammatical lexical analysis, expression evaluation, etc.
2. Queues (Queue) is also a limited operation of the linear table, its operation limit is different from the stack, there are two restrictions, the insertion can only be done at one end of the table (only not in), and the deletion can only be done at the other end of the table (only out), allowing the deletion of one end called the tail of the queue (rear), Front), the operational principle of the queue is FIFO, so the queue is also referred to as the first-out table.
Because stacks and queues are also linear tables, stacks and queues have sequential stacks and chain stacks of two storage structures, and the two storage structures differ in the algorithm that implements the basic operations of the stack.
Two. JS Stack study
1. Stack (stack) and 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
(1) 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.
(2) 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 explanation for what is in-memory heap, stack, and variable types is actually a better understanding of what is "shallow copy" and "Deep copy".
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:
var a = [1,2,3,4,5];var b = A;var c = A[0];alert (b);//1,2,3,4,5 alert (c);//1//Change value b[4] = 6;c = 7;alert (a[4]);//6aler T (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.
Three. Copy
1. 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.
var a = { key1: "11111" }function Copy (p) {var c = {};for (var i in P) { c[i] = P[i]; } return C; } a.key2 = [' Little Fai ', '];var ' B = Copy (a); B.key3 = ' 33333 '; alert (b.key1); 1111111 alert (b.key3); 33333 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 you add a property Key3 for a string type to a B object, B changes 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.
B.key2.push ("Da Hui"); alert (b.key2); Xiao Hui, Xiao Fai, Big FAI 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.
2. 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 is only the address, then we use recursion to solve the 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:
function Copy (p, c) {var c = c | | {};for (var i in P) {if (typeof p[i] = = = ' object ') { c[i] = (P[i].constructor = = = Array)? [] : {}; Copy (P[i], c[i]); } else { c[i] = p[i]; } }return C; } a.key2 = [' Xiao Hui ', ' Xiao Hui '];var b={}; b = Copy (A, b); B.key2.push ("Da Hui"); alert (b.key2); Xiao Hui, Xiao Fai, Big FAI 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:
JS Stack and copy