Home >Web Front-end >JS Tutorial >JavaScript data types: Basic types and reference type values_Basic knowledge

JavaScript data types: Basic types and reference type values_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:06:181056browse

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:

Copy code The code is as follows:

var person = new Object();
person.name = "zxj";
alert(person.name); //"zxj" 

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.

Copy code The code is as follows:

var num1 = 5;
var num2 = num1; //5

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:

Copy code The code is as follows:

var obj1 = new Object();
var obj2 = obj1;
obj1.name = "zxj";
alert(obj2.name); //"zxj"

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:

Copy code The code is as follows:

function addTen(num) {
num = 10;
Return num;
}
var count = 20;
var result = addTen(count);
alert(count); // 20, no change
alert(result); // 30

Parameters are actually local variables of the function. The parameter num and the variable count do not know each other, they just have the same value. If num is passed by reference, the value of the variable count will also become 30, reflecting the changes within the function.

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:

Copy code The code is as follows:

function setName(obj) {
Obj.name = "zxj";
}
var person = new Object();
setName(person);
alert(person.name); //"zxj"

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:

Copy code The code is as follows:

function setName(obj) {
Obj.name = "zxj";
Obj = new Object();
Obj.name = "sdf";
}
var person = new Object();
setName(person);
alert(person.name);

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:

Copy code The code is as follows:

result = variable instanceof constructor

If the variable is an instance of the given reference type, the instanceof operator will return true:
Copy code The code is as follows:

alert(person instanceof Object);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn