The basic type of JavaScript data type and the value of reference type _ basics

Source: Internet
Author: User

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.

When assigning a value to a variable, the parser must determine whether the value is a base type or a reference type. Basic types include, for example, Undefined, Null, Boolean, number, and string, the 5 basic types of data types are accessed by value, so you can manipulate the actual values that are saved in the variable; the value of the reference type type is the object that is saved in memory. Unlike other languages, JavaScript does not allow direct access to the location in memory, which means that the object's memory space cannot be manipulated directly. When manipulating an object, you actually manipulate the object's reference rather than the actual object, so the value of the reference type is accessed by reference.

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:

Copy Code code as follows:

var person = new Object ();
Person.name = "ZXJ";
alert (person.name); "Zxj"

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.

Copy Code code as follows:

var num1 = 5;
var num2 = NUM1; 5

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:

var obj1 = new Object ();
var obj2 = obj1;
Obj1.name = "ZXJ";
alert (obj2.name); "Zxj"

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:

Copy Code code as follows:

function Addten (num) {
num + + 10;
return num;
}
var count = 20;
var result = Addten (count);
alert (count); 20, no change.
alert (result); 30

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:

Copy Code code as follows:

function SetName (obj) {
Obj.name = "ZXJ";
}
var person = new Object ();
SetName (person);
alert (person.name); "Zxj"

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:

Copy Code code as follows:

function SetName (obj) {
Obj.name = "ZXJ";
obj = new Object ();
Obj.name = "SDF";
}
var person = new Object ();
SetName (person);
alert (person.name);

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:

result = Varible Instanceof constructor

If the variable is an instance of the given reference type, then the instanceof operator returns true:
Copy Code code as follows:

Alert (person instanceof Object);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.