Basic JavaScript data type, javascript Data Type
As I am a wild programmer, I didn't care about the basic knowledge of memory when I first started learning program design. As a result, I later mentioned, "what is in the stack, there is only a reference in the stack ..
Later, I gradually learned some knowledge about memory, which is quite necessary.
Basic Data Structure
Stack
Stack, which only allows insert or delete linear tables in a segment. It is an advanced and post-release data structure.
Heap
Heap is a data structure based on the hash algorithm.
Queue
A queue is a data structure of first-in-first-out (FIFO.
Storage of Data Types in JavaScript
In JavaScript, data types are divided into basic data types and reference data types. The difference is that the storage location is different.
Basic Data Type
We all know that the basic data types in JavaScript include:
- String
- Number
- Boolean
- Undefined
- Null
- Symbol (temporarily ignore)
The basic data types are some simple data segments, which are stored in the stack memory.
Reference data type
The reference data types in JavaScript include:
The reference data type is stored in the heap memory, and a reference to the actual object in the heap memory is saved in the stack memory. Therefore, operations on the referenced data type in JavaScript are the reference of the operation object rather than the actual object.
It can be understood that the stack memory stores an address, which is related to the actual value in the heap memory.
Illustration
Now, let's declare several variables and try:
var name="axuebin";var age=25;var job;var arr=[1,2,3];var obj={age:25};
It can be used to indicate the storage of data types in the memory:
In this casename,age,jobThree basic data types exist directly in the stack memory, while arr and obj only store an address in the stack memory to indicate references to the stack memory.
Copy
Basic Data Type
For the basic data type, if you copy the data, the system automatically allocates a new value to the new variable in the stack memory, which is easy to understand.
Reference data type
If the referenced data types such as arrays and objects are different during data replication:
The system automatically allocates a value to the stack memory for the new variable, but this value is only an address. That is to say, the copied variable has the same address value as the original variable, pointing to the same object in the heap memory.
As shown in, after var objCopy = obj is executed, obj and objCopy have the same address value and execute the same actual object in the heap memory.
What is the difference?
When I modify obj or objCopy, it will change another variable.
Why?
Why does the basic data type exist in the stack and the referenced data type exist in the heap?
- The stack is larger than the stack, and the stack is faster than the stack.
- The basic data type is relatively stable, and the memory usage is relatively small.
- The size of the referenced data type is dynamic and unlimited.
- The heap memory is unordered and can be directly obtained by reference.
References
Understanding js Memory Allocation
Original Value and reference value
In ECMAScript, variables can be stored in two types of values: Original Value and reference value.
The original value indicates the value of the original data type (basic data type), that is, the value of the Undefined, Null, Number, String, and Boolean types.
Reference Value refers to the value of the composite data type, that is, Object, Function, Array, and custom Object.
Stack and stack
There are two types of memory corresponding to the original value and reference value: Stack and heap.
Stack is a type of data structure that is later-in-first-out. In javascript, Array can be used to simulate stack behavior.
Raw values are simple data stored in the stack, that is, their values are directly stored in the variable access location.
Heap is a data structure based on the hash algorithm. In javascript, reference values are stored in the heap.
The reference value is an object stored in the heap, that is, the value stored in the variable (that is, the variable pointing to the object, stored in the stack) is a pointer, point to the actual object stored in the heap.
For example, var obj = new Object (); obj is stored in the stack and points to the Object new Object (), while new Object () is stored in the heap.
So why should the reference values be placed in the heap, and the original values be placed in the stack, not in the memory? Why not put them together? Next, let's explore the answer to the question!
First, let's look at the Code:
Function Person (id, name, age) {this. id = id; this. name = name; this. age = age;} var num = 10; var bol = true; var str = "abc"; var obj = new Object (); var arr = ['A ', 'B', 'C']; var person = new Person (100, "stupid motto", 25 );
Then let's take a look at the memory analysis diagram:
The num, bol, and str variables are the basic data types. Their values are directly stored in the stack. obj, person, and arr are the composite data types, and their reference variables are stored in the stack, point to the actual object stored in the heap.
As we can see, we cannot directly manipulate the data in the heap, that is to say, we cannot directly manipulate the object, but we can operate the object through referencing the object in the stack, just as we operate the TV through a remote control machine, the difference is that the TV itself has no control buttons.
Now let's answer why the reference value should be placed in the heap and the original value should be placed in the stack:
Remember one sentence: energy is constant. It is nothing more than time-for-space or space-for-time.
The heap is larger than the stack, and the stack is faster than the heap operation speed. The object is a complex structure and can be expanded freely. For example, the array can be expanded infinitely, and the object can be added freely. Put them in the heap to avoid affecting the efficiency of the stack. Instead, find the actual object in the heap by referencing it and then perform the operation. Compared with simple data types, simple data types are relatively stable and occupy only a small amount of memory. Not putting simple data types in the heap is because it takes time to reference the actual object in the heap, and the overall cost is much greater than the cost of directly obtaining the actual value from the stack. Therefore, values of the simple data type are directly stored in the stack.