The ECMAScript variable contains values for two different data types: the base type value and the reference type value. A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may consist of multiple values. When manipulating the base data type, it is the actual value that is saved in the variable, that is, access by value. When you manipulate a reference data type, it is the reference to the object that is manipulated instead of the actual object (when you copy a variable that holds an object, the action is a reference to it.) However, when you add a property to an object, you are manipulating the actual object, which is accessed by reference.
Dynamic Properties
You define a primitive type value and a reference type value in a similar way: Create a variable and assign a value to the variable. However, when this value is saved to a variable, the actions that can be performed on the different types of values are quite distinct. For values of reference types, we can add properties and methods to them, and you can change and delete their properties and methods. The value of the base data type is not possible (although doing so does not cause any errors).
1 {2 //Reference Data Type3 varperson =NewObject ();4Person.name = ' Nicholas ';5Console.log (Person.name);//"Nicholas"6 //Basic data Types7 varname = ' Nicholas ';8Name.age = 27;9Console.log (Name.age);//undefinedTen}
Copy variable values
In addition to the way it is saved, there is a difference between copying the base type value and the reference type value from one variable to another. If you copy the 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.
1 {2 var num1 = 5; 3 var num = num1; 4 }
The procedure for copying a base type value is as follows,
When a value of a reference type is copied from one variable to another, the value stored in the variable object is also copied into the space allocated for the new variable. The difference is that a copy of this value is actually a pointer. And this pointer points to an object that is stored in the heap. When the copy operation is finished, two variables will actually refer to the same object. Therefore, changing one of the variables will affect the other variable.
1 {2 var New New Object (); 3 var obj2 = obj1; 4 Obj1.name = ' Nicholas '; 5 Console.log (Obj2.name); // "Nicholas" 6 }
The relationships between the variables saved in the variable object and the objects held in the heap are as follows,
Passing parameters
The parameters of all functions in ECMAScript are passed by value. That is, copying the values outside the function to the parameters inside the function is the same as copying the value from one variable to another. The delivery of a primitive type value is like a copy of a primitive type variable, whereas a reference type value is passed as a copy of a reference type variable.
When you pass a value of a primitive type to a parameter, the passed value is copied to a local variable. When a value of a reference type is passed to a parameter, the address of the value is copied to a local variable, so the change of the local variable is reflected outside the function.
1 {2 //passing basic data types3 functionAddten (num) {4num + = 10;5 returnnum;6 }7 varCount = 20;8 varresult =Addten (count);9Console.log (count);// -TenConsole.log (result);// - One //passing reference data types A functionsetName (obj) { -Obj.name = ' Lili '; - } the varperson =NewObject (); - setName (person); -Console.log (Person.name);//"Lili" -}
JavaScript-----data type values for variables and variables