Base data types and reference data types
ECMASciprt contains two different types of values: the base data type and the reference data type.
Basic data types refer to simple data segments, and reference data types refer to objects with multiple values.
When we assign a variable to a variable, the first thing the parser needs to determine is whether the value is a primitive or a reference type.
2. Common basic data types:
Number, String, Boolean, Null, and Undefind. Basic data types are accessed by value because the actual values stored in variables can be manipulated directly. Example: var a = 10; var b = a ; b = 20 ; console.log(a); / / 10 values
Above, B gets a copy of a’s value. Although the values of the two variables are equal, they hold two different values of the basic data type.
B only keeps a copy of a’s copy. So the change in B has no effect on A.
The following illustration illustrates the assignment of this basic data type:
3. Reference type data
Object type: Object, Array, Function, Data, etc.
JavaScript reference data types are objects stored in heap memory.
Unlike other languages, you cannot directly access locations in the heap space and manipulate the heap space. Only reference addresses of objects in stack memory can be manipulated.
Therefore, the reference type data is actually held in stack memory as the reference address of the object in heap memory. This reference address is used to quickly locate objects in the saved heap memory. var obj1 = new Object(); var obj2 = obj1; Obj2. name = “I have a name “; console.log(obj.name); // I have a name
The two reference data types refer to the same heap memory object. Obj1 “assigns to” obj2 “, which actually makes a copy of the heap object at its stack reference address to obj2, but in fact they both refer to the same heap object. What actually changes is the heap memory object.
Let’s demonstrate the process of assigning a reference data type:
4. Summarize the differences
A Different memory allocation when declaring variables:
**1) Raw values: ** simple data segments are stored in the stack, that is, their values are stored directly where the variables are accessed. This is because these primitive types take up a fixed amount of space, so they can be stored in a small area of memory – the stack. This storage facilitates quick lookup of the value of the variable.
**2) Reference value: ** An object stored in the heap, that is, the value stored in a variable is a pointer to the memory address where the object is stored. This is because the size of the reference value changes, so it cannot be placed on the stack, which would slow down variable queries. Instead, the value placed in the stack space of a variable is the address of the object stored in the heap. The size of the address is fixed, so storing it on the stack has no negative impact on variable performance.
B Different memory allocation mechanisms lead to different access mechanisms
**1) ** JavaScript does not allow you to access objects stored in the heap directly, so when you access an object, you first get the address of the object in the heap and then use that address to retrieve the value of the object. This is called access by reference.
**2) ** Whereas primitive values are directly accessible.
C The difference between copying variables
1) Original value: When a variable holding the original value is copied to another variable, the copy of the original value is assigned to the new variable. The two variables are completely independent, they just have the same value.
** When a variable holding the original value is assigned to another variable, the memory address is assigned to the new variable. That is, both variables refer to the same object in memory, and changes made by either of them are reflected by the other. (One thing to understand here is that copying an object does not create a new identical object in the heap, just a variable that holds a pointer to that object.)
D Differences in parameter passing (the process of copying arguments to parameters)
First, let’s be clear: all functions in ECMAScript are passed parameters by value.
But why is there still a difference when it comes to values of primitive and reference types? Because of differences in memory allocation.
**1) Original value: ** just passes the value of the variable to the parameter, and the parameter and the variable are not affected.
** The value in the object variable is the memory address of the object in the heap.
Because the value it passes is the memory address, this is why the internal changes to this parameter are external, because they all point to the same object.