Basic type and reference type of JavaScript Data Type _ basic knowledge

Source: Internet
Author: User
This article mainly introduces the basic types and reference types of JavaScript data types. This article describes dynamic attributes, copy variable values, transfer parameters, and detection types, you can refer to the ECMAScript variable that contains two different data types: basic type value and reference type value. A basic type value is a simple data segment, and a reference type value is an object that may consist of multiple values.

When you assign a value to a variable, the parser must determine whether the value is of the basic type or reference type. Basic types include Undefined, Null, Boolean, Number, and String. These five basic data types are accessed by value. Therefore, you can operate on the actual values stored in the variables; the value of the reference type is an object stored in memory. Unlike other languages, JavaScript does not allow direct access to locations in the memory, that is, it cannot directly operate on the memory space of objects. When operating on an object, the actual operation object is referenced rather than the actual object. Therefore, the value of the reference type is accessed by reference.

1. dynamic attributes
The methods for defining basic types and defining reference types are similar. For values of the reference type, we can add attributes and methods for them, or modify and delete their attributes and methods, as shown below:

The Code is as follows:


Var person = new Object ();
Person. name = "zxj ";
Alert (person. name); // "zxj"

2. Copy the variable value

If you copy a value of the basic type from one variable to another, a new value is created on the variable object, and then the value is copied to the location assigned to the new variable.

The Code is as follows:


Var num1 = 5;
Var num2 = num1; // 5

When a value of the reference type is copied from one variable to another, the value stored in the variable object is also copied to the memory space allocated for the new variable. The difference is that this value is actually a pointer, and this pointer executes an object stored in the heap. After the replication, the two variables actually reference the same object. Therefore, changing one of the variables will affect another, as shown below:

The Code is as follows:


Var obj1 = new Object ();
Var obj2 = obj1;
Obj1.name = "zxj ";
Alert (obj2.name); // "zxj"

3. PASS Parameters

Parameters of all functions in ESMAScript are passed by value. That is to say, copying the external value of the function to the parameter inside the function is to copy the value from one variable to another. The transfer of the basic type value is the same as the replication of the basic type variable. The transfer of the reference type value is the same as the copy of the reference type variable. Many developers may be confused about this, because there are two ways to access variables: by value and by reference, and parameters can only be passed by value.

When you pass a value of the basic type to a parameter, the passed value is copied to a local variable (named parameter ). The following code is used:

The Code is as follows:


Function addTen (num ){
Num + = 10;
Return num;
}
Var count = 20;
Var result = addTen (count );
Alert (count); // 20, unchanged
Alert (result); // 30


The parameter is actually a local variable of the function. The num parameter and the 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 be changed to 30, reflecting the internal changes of the function.

When a value of the reference type is passed to a parameter, the address in the memory of the value is copied to a local variable. Therefore, the change of this local variable is reflected outside the function. Here we use the reference type to look:

The Code is as follows:


Function setName (obj ){
Obj. name = "zxj ";
}
Var person = new Object ();
SetName (person );
Alert (person. name); // "zxj"

In this function, obj and person reference the same object. In other words, even if this object is passed by value, obj accesses the same object by reference. Therefore, after adding the name attribute to the obj in the function, the person outside the function will also reflect it, because the person points to only one object in the heap memory and is a global object. Many developers mistakenly think that the objects modified in the local scope will be reflected in the global scope, which means that the parameters are passed by reference. To prove that the object is passed by value, let's look at the following modified example:

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 shown in the preceding example, if a person is passed by reference, the person will be automatically changed to a new object pointing to its name attribute value "sdf. However, when you access person. name again, "zxj" is displayed ". This indicates that the original reference remains unchanged even if the parameter value is modified within the function. In fact, when you override obj in a function, this variable references a local object. This local object will be destroyed immediately after the function is executed.

You can imagine the parameters of the ECMAScript function as local variables.

4. Detection type

Although typeof is a good helper when detecting basic data types, this operator is not very useful when detecting reference types. Generally, we do not want to know that a value is an object. Instead, we want to know what type of object it is. Therefore, ECMAScript provides the instanceof operator. Its syntax is as follows:

The Code is as follows:


Result = varible instanceof constructor


If the variable is an instance of the given reference type, the instanceof operator returns true:

The Code is 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.