variables, scopes, and memory issues
Values for base types and reference types
The ECMAScript variable may contain 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 assigning a value to a variable, the parser must determine whether the value is a base type value or a reference type value. The 3rd chapter discusses 5 basic data types: Undefined, Null, Boolean, number, and string. These 5 basic data types are accessed by value because you can manipulate the actual values that are saved in the variable.
the value of a reference type is an object that is saved in memory . Unlike other languages, JavaScript does not allow direct access to in-memory locations, which means that the object's memory space cannot be manipulated directly. When you manipulate an object, you are actually manipulating the object's reference rather than the actual object.
To do this, the value of the reference type is ① accessed by reference.
Note: In many languages, strings are represented as objects and are therefore considered reference types. ECMAScript has given up on this tradition.
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 . Take a look at the following example:
var person = new Object ();
Person.name = "Nicholas";
alert (person.name); "Nicholas"
The above code creates an object and saves it in the variable person. We then added a property named name to the object and assigned the string value "Nicholas" to this property. The new property is then accessed through the alert () function. This property will persist if the object is not destroyed or if the property is not deleted.
However, we cannot add properties to the values of the base type, although doing so does not result in any errors. Like what:
var name = "Nicholas";
Name.age = 27;
alert (name.age); Undefined
In this example, we define a property named age for the string name and assign a value of 27 to the property. However, when this property is accessed on the next line, the attribute is found to be missing. This means that you can add properties dynamically to reference type values for future use.
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. Take a look at an example:
var num1 = 5;
var num2 = NUM1;
In this case, the value saved in NUM1 is 5. When NUM2 is initialized with the value of NUM1, the value 5 is also saved in num2. However, 5 in num2 and 5 in NUM1 are completely independent, and this value is just a copy of 5 in NUM1. Thereafter, these two variables can participate in any operation without affecting each other. Figure 4-1 Visually shows the process of copying the base type values.
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 to an object 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, as shown in the following example:
var obj1 = new Object ();
var obj2 = obj1;
Obj1.name = "Nicholas";
alert (obj2.name); "Nicholas"
First, the variable obj1 holds a new instance of an object. The value is then copied to the Obj2; in other words, both obj1 and Obj2 point to the same object. This way, when the Name property is added for Obj1, this property can be accessed through obj2 because both variables refer to the same object. Figure 4-2 shows the relationship between the variables saved in the variable object and the objects held in the heap.
Passing parameters
The parameters of all functions in ECMAScript are passed by value. In other words, copying the values from the outside of 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 to a value of a type is the same as a copy of a reference type variable. There are a number of developers who may be confused at this point, because access variables are both by value and by reference, and parameters can only be passed by value .
When you pass a value of a primitive type to a parameter, the passed value is copied to a local variable (that is, a named parameter, or, in the ECMAScript concept, an element in the arguments object). When a value of a reference type is passed to a parameter, the address of the value in memory is copied to a local variable, so the change of the local variable is reflected outside the function. Take a look below
This example:
function Addten (num) {
num + = 10;
return num;
}
var count = 20;
var result = Addten (count);
alert (count); 20, no change
alert (result); 30
Here the function Addten () has a parameter num, and the argument is actually a local variable of the function. When this function is called, the variable count is passed as a parameter to the function, and the value of the variable is 20. The value 20 is then copied to the parameter num for use in Addten (). Inside the function, the value of the parameter num is added to 10, but this change does not affect the count variable outside the function. Parameter num is not acquainted with the variable count, they simply have the same value. If NUM is passed by reference, then the value of the variable count is also changed to 30, which reflects the changes inside the function. Of course, using basic types such as numeric values to illustrate that passing parameters by value is simple, but if you use objects, the problem is not very well understood. One more example:
function SetName (obj) {
Obj.name = "Nicholas";
}
var person = new Object ();
SetName (person);
alert (person.name); "Nicholas"
The above code creates an object and saves it in the variable person. The variable is then passed to the SetName () function and is then copied to obj. Inside this function, obj and person refer to the same object. In other words, even if the variable is passed by value, obj accesses the same object by reference. Therefore, when you add the Name property to obj inside the function, the person outside the function will also be reflected, because the object pointed to by person has only one and global object in the heap memory. There are many developers who mistakenly assume that objects modified in a local scope are reflected in the global scope, stating that the parameters are passed by reference. To prove that the object is passed by value, let's take a look at the following modified example:
function SetName (obj) {
Obj.name = "Nicholas";
obj = new Object ();
Obj.name = "Greg";
}
var person = new Object ();
SetName (person);
alert (person.name); "Nicholas"
The only difference between this example and the previous example is that two lines of code are added to the SetName () function: One line of code redefined an object for obj, and the other line defines a name property with a different value for the object. After the person is passed to SetName (), its Name property is set to "Nicholas". Then, assign a new object to the variable obj, and set its Name property to "Greg". If person is passed by reference, the person is automatically modified to point to a new object whose Name property has a value of "Greg". However, when you next access Person.name, the value displayed is still "Nicholas". This means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when you rewrite obj inside a function, the variable refers to a local object. This local object is destroyed immediately after the function is executed.
JavaScript Advanced Program Design reading notes---Basic types and values of reference types