Do you really understand how data is stored in the stack?

Do you really understand how data is stored in the stack?


JavaScript data types are divided into:

classification Types of
Original data type (7 types) Number、String、Boolean、Null、Undefined、Symbol、BigInt
Reference data type Object

Look at two pieces of code first

let num = 1;
let data = num;
data = 2;
console.log('num : ' + num);//num : 1
let obj = {
    
    
    name : 'obj'
}
let data = obj;
data.name = 'data';
console.log('obj.name : ' + obj.name);//obj.name : data

This situation occurs because of the different storage methods of primitive data types and reference data types in JavaScript, leading
to the introduction of two concepts here: stack and heap

Storage method advantage Disadvantage
Stack The storage size is fixed, and it has the linear operation function of last in, first out, which is convenient for operating the data in the storage. Small storage capacity
Heap Large storage capacity The storage size is not fixed, and it is not easy to manipulate the data in the storage without knowing the memory address

Note: The advantages and disadvantages here are only relative

Look again

classification Features
Primitive data type Requires a fixed memory size
Reference data type Need not fixed memory size

So the original type is stored in the stack

let num = 1;
let data = num;
data = 2;
console.log('num : ' + num);//num : 1

The specific process is as follows:
1. Open up a piece of memory in the stack and store the value 1 in it, declare the variable num so that num points to the newly opened memory address;
2. Open up another piece of memory in the stack, and point the variable num to Copy the value in the memory to the newly opened memory, declare the variable data so that data points to the newly opened memory address;
3. Replace the value in the memory pointed to by the variable data with 2;
4. Output the memory pointed to by the variable num The value stored in.

As shown in the figure:
Insert picture description here

The data of the reference data type is stored in the heap and the address value of the data stored in the heap is stored in the stack

let obj = {
    
    
    name : 'obj'
}
let data = obj;
data.name = 'data';
console.log('obj.name : ' + obj.name);//obj.name : data

The specific process is as follows:
1. Open up a piece of memory in the heap, store the object in it, open up a piece of memory in the stack, store the address value of the newly opened memory in the stack, declare the variable obj, make obj point to the new stack The opened address value;
2. Create another memory in the stack, copy the value in the memory pointed to by obj to the newly opened memory, declare the variable data, and make data point to the newly opened memory address;
3 . Through the address value of the heap memory stored in the stack memory pointed to by the variable data, modify the name attribute of the data in the heap to data; 4. Output the name attribute of the data
in the address value of the heap memory stored in the stack memory pointed to by the variable obj value.

As shown in the figure:
Insert picture description here
there is a supplement

let obj = {
    
    
    name : 'obj'
}
let data = obj;
data = {
    
    
	name : 'data'
}
console.log('obj.name : ' + obj.name);//obj.name : obj

The specific process is as follows:
1. Open up a piece of memory in the heap, store the object in it, open up a piece of memory in the stack, store the address value of the newly opened memory in the stack, declare the variable obj, make obj point to the new stack The opened address value;
2. Create another memory in the stack, copy the value in the memory pointed to by obj to the newly opened memory, declare the variable data, and make data point to the newly opened memory address;
3 Open up a piece of memory in the heap, store the object in it, and store the address value of the newly opened memory in the address value of the variable data pointing to the stack;
4. Output the address value of the heap memory stored in the stack memory pointed to by the variable obj The value of the name attribute of the data.

As shown in the figure:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35508835/article/details/108836092