The ECMAScript variable contains values of two different data types: the base type value and the reference type value. The base type value is a simple data segment, whereas a reference type value refers to objects that may be composed of multiple values.
1, the dynamic properties
Methods for defining basic types and defining reference types big close court. For values of reference types, we can add properties and methods to them, as well as change and delete their properties and methods, as follows:
2. Copy variable Value
If you copy a value of the base type from one variable to another, a new value is created on the variable object, and the value is copied to the location assigned to the new variable.The code is as follows:
When you copy a value of a reference type from one variable to another, the value stored in the variable object is also copied into the memory space allocated for the new variable. The difference is that this value is actually a pointer, which executes an object stored in the heap. After the copy is finished, two variables will actually refer to the same object. Therefore, changing one of these variables will affect another variable, as follows:copy code code as follows:
3, transfer parameters
Parameters for all functions in Esmascript are passed by value. That is, copying the values from outside the function to the parameters inside the function is to copy the value from one variable to another. The delivery of the base type value is the same as the copy of the base type variable. A reference to the delivery of a type value is the same as a copy of a reference type variable. There are a number of developers who may be puzzled at this point because access variables are both by value and by reference, and parameters can only be passed by value.
When a value of the base type is passed to a parameter, the passed value is copied to a local variable (that is, a named parameter). As shown in the following code:The code is as follows:
A parameter is actually a local variable of a function. Parameter num and variable count do not know each other, they only have the same value. If NUM is passed by reference, the value of the variable count will also change to 30, reflecting changes within the function.
When a value of a reference type is passed to a parameter, the value is copied to the local variable in the address in memory, so the change of the local variable is reflected outside the function. Here we use the reference type to look at:The code is as follows:
Within this function, obj and person refer to the same object. In other words, even if the object is passed by value, obj accesses the same object by reference. Then, when the name attribute is added to obj inside the function, the person outside the function is also reflected, because the object that person points to is only one in the heap memory and is a global object. Many developers mistakenly believe that objects modified in a local scope are reflected in the global scope, indicating that the parameters are passed by reference. In order to prove that the object is passed by value, we are looking at the following example that has been modified:The code is as follows:
As you can see from the example above, if a person is passed by reference, the person is automatically modified to point to a new object whose Name property value is "SDF". However, when you next visit Person.name, the "Zxj" is still displayed. This means that even though the value of the parameter has been modified inside the function, the original reference remains unchanged. In fact, when you rewrite obj inside a function, this variable refers to a local object. This local object is destroyed as soon as the function is finished executing.
The parameters of the ECMAScript function can be imagined as local variables.
4, detection type
Although TypeOf is a right-hand man when detecting basic data types, this operator is not useful when detecting reference types. Often, we don't want to know that a value is an object, but rather to know what kind of object he is. For this ECMAScript, the instanceof operator is provided with the following syntax:copy code code as follows:
copy code code as follows:
If the variable is an instance of the given reference type, then the instanceof operator returns true: