ECMAScript variables contain values of two different data types: basic type values and reference type values. Primitive type values are simple pieces of data, while reference type values are objects that may be composed of multiple values.
When assigning a value to a variable, the parser must determine whether the value is a primitive type or a reference type. Basic types include Undefined, Null, Boolean, Number and String. These five basic data types are accessed by value, so the actual value stored in the variable can be manipulated; the value of the reference type is stored in memory. object. Unlike other languages, JavaScript does not allow direct access to locations in memory, which means that the memory space of an object cannot be directly manipulated. When operating on an object, you actually operate on a reference to the object rather than the actual object, so the value of a reference type is accessed by reference.
1. Dynamic attributes
The methods of defining basic types and defining reference types are very similar. For reference type values, we can add properties and methods to them, and we can also change and delete their properties and methods, as follows:
2. Copy variable value
If you copy a value of a basic type from one variable to another, a new value is created on the variable object and then copied to the location allocated for the new variable.
When a reference type value is copied from one variable to another variable, a copy of the value stored in the variable object will also be copied into the memory space allocated for the new variable. The difference is that this value is actually a pointer to an object stored in the heap. After copying is complete, both variables will actually refer to the same object. Therefore, changing one of the variables will affect the other variable, as shown below:
3. Pass parameters
The parameters of all functions in ESMAScript are passed by value. In other words, copying the value outside the function to the parameter inside the function is the same as copying the value from one variable to another. Primitive type values are passed just like primitive type variables are copied. The transfer of reference type values is the same as the copying of reference type variables. Many developers may be confused at this point, because there are two ways to access variables, by value and by reference, while parameters can only be passed by value.
When passing a basic type value to a parameter, the passed value will be copied to a local variable (named parameter). As shown in the following code:
When passing a reference type value to a parameter, the address of the value in memory will be copied to a local variable, so changes in this local variable will be reflected outside the function. Here we use reference types to take a look:
Inside this function, obj and person refer to the same object. In other words, even if the object is passed by value, obj will access the same object by reference. Therefore, when the name attribute is added to obj inside the function, the person outside the function will also be reflected, because there is only one object pointed to by person in the heap memory, and it is a global object. Many developers mistakenly believe that objects modified in the local scope will be reflected in the global scope, which means that parameters are passed by reference. To prove that objects are passed by value, let’s look at the following modified example:
As can be seen from the above example, if person is passed by reference, then person will automatically be modified to point to a new object whose name attribute value is "sdf". However, when person.name is accessed next, "zxj" is still displayed. This shows that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when obj is overridden inside a function, this variable refers to a local object. This local object will be destroyed immediately when the function completes execution.
Think of the parameters of ECMAScript functions as local variables.
4. Detection type
Although typeof is a powerful assistant when detecting basic data types, this operator is not very useful when detecting reference types. Usually, we don't want to know whether a value is an object, but what type of object it is. ECMAScript provides the instanceof operator for this purpose, whose syntax is as follows: