Here are the first two concepts:1, Heap (heap)2, Stack (stack)
A heap is a short name for heap memory.
The stack is the short name of the stack memory.
When it comes to stacks, we're talking about the use and allocation of memory, the fact that there are no registers, and no hard drives.
The principles of processing stacks are similar in all languages. The heap is dynamically allocated memory, the size of the memory varies, and it is not automatically freed. The stack is automatically allocated a relatively fixed size of memory space and is automatically released by the system.
There are 5 basic types of javascript: Undefined, Null, Boolean, number, and string, which are stored directly in the stack by value, and the size of the memory space used by each type of data is determined and automatically allocated and freed by the system. The benefit is that memory can be recycled in a timely manner, and it is easier to manage memory space relative to the heap.
Other types of data in JavaScript are referred to as reference types of data: such as objects (object), arrays (array), functions (function) ..., which are stored in the heap by copying and new. In fact, it is not very accurate to say that stored in the heap, because the address pointer of the reference type data is stored in the stack, when we want to access the value of the reference type, we need to get the object's address pointer from the stack, and then, in the address pointer to find the required data in the heap.
It is also image, stack, linear structure, LIFO, easy to manage. Heap, a chaotic, disorganized, easy to store and open up memory space
Transmit Value and address
var arr1 = [1,2,5,8]; var arr2 = arr1 ; var str1 = arr1[2]; console.log(arr2);//1,2,5,8console.log(str1);//5arr2[4] = 99; str1 = 6; console.log(arr1);//1,2,5,8,99console.log(arr1[2]);//5
The above example learns that when I change the data in the ARR2, the data in the arr1 also changes, and when the STR1 data value is changed, ARR1 does not change. Why? This is the difference between the value of the transmission and the address.
Because ARR1 is an array and is a reference type, it is given to arr2 by the address in the stack (the equivalent of creating a new "pointer" without the same name), rather than the value of the object in the heap memory. STR1 is given a basic type of assignment, so str1 just gets a value from the arr1 heap memory and stores it directly in the stack. ARR1, arr2 all point to the same heap of memory, arr2 modified heap memory, it will affect the ARR1,STR1 is directly in the stack, and can not affect the arr1 heap in memory data.
Shallow copy and deep copy
The above-mentioned assignment is a shallow copy, so what is called a deep copy? is to iterate through the data of each of the basic types of arr1, assigning the corresponding fields of the arr2 to the values in turn. Avoid problems caused by address references.
var arr1 = [1,2,5,8]; var arr2 = []; for(var i=0;i<arr1.length;i++){ arr2[i]=arr1[i];};console.log(arr2)//1,2,5,8arr2[4]=99;console.log(arr2)//1,2,5,8,99console.log(arr1)//1,2,5,8
The JavaScript object-oriented language itself is divided between the processing object and the non-object, from the data structure point of view, the object is the stack of pointers and values in the heap.
The contents of this paper draw from
Link: https://www.jianshu.com/p/5e0 ...
Understanding the heap and stack in JavaScript