Difference analysis of basic types and reference types in JavaScript _javascript tips

Source: Internet
Author: User

Basic types and reference types

ECMAScript contains two different types of values: the base type value and the reference type value. A base type value refers to a simple data segment; A reference type value is an object consisting of multiple values. When we assign a variable to a variable, the first thing the parser does is confirm whether the value is a base type value or a reference type value.

The five basic types of data that are common are:

undifined, Null, Boolean, number, and string. These five basic data types can directly manipulate the actual values saved in the variable.

Look at the following example:

var a = ten;
var b = A;
   b =;
Console.log (a);
    
var bl = true;
var bl1 = bl;
   BL1 = false;
Console.log (BL); True

Above, B gets a copy of a value, although the value of two variables is equal, but two variables hold two different base data type values. b just saved a copy of a copy. So, when the value of B changes, the value of a is still 10;
Below, the two Boolean variable bl and BL1 are also basic data types, and also hold two different base data type values, BL1 save a copy of the BL copy.

The following illustration shows the process of assigning this basic data type:

Look at the reference type data below:

JavaScript reference data types are objects that are stored in heap memory and, unlike other languages, you cannot directly access the location in the heap memory space and the operating heap memory space. Only the reference address in the stack memory of the object is manipulated. So the reference type of data, stored in the stack memory is actually the object in the heap memory reference address. This reference address allows you to quickly find objects that are saved in heap memory.

Look at the following example:

var obj1 = new Object ();
var obj2 = obj1;
Obj2.name = "I have a name";
Console.log (Obj1.name); I have a name.

From the above example, we declare a reference to the data type variable obj1 and assign it to another reference data type variable OBJ2. When we obj2 added a name attribute and assigned "I have a name." OBJ1 also has the same name property as Obj2. Description The two reference data type variables point to the same heap memory object. The obj1 assignment to Obj2 is actually a copy of the heap memory object's reference address in the stack memory to the OBJ2, but they essentially point together to the same heap memory object.

Let's demonstrate this reference data type assignment procedure:

Naturally, adding the name attribute to the OBJ2 is actually adding the name attribute to the object in the heap memory, and OBJ2 and obj1 are storing only the reference addresses of the heap memory objects in the stack memory, although they are copied, but the same object is pointed to. Therefore, the change of OBJ2 caused obj1 change.

In general, the base data type is composed of a fixed number of bytes that can be manipulated at the lower level of the parser such as number and Boolean, whereas reference data types can contain as many properties and elements as possible, so they cannot operate as easily as the basic data type. Since the value of the reference data type changes, it makes little sense to pass the same value as the basic data type, because it involves a lot of memory duplication and comparison, and is inefficient. So the reference data type is passed by reference, which actually passes only one address of the object. For example, array and function because they are all special objects, so it is a reference type. In addition, a reference type is a property that can be added, although the base type can also add attributes and do not have an error, but is not accessible after the test has been added.

Look at the following code:

var a =;
 A.name = "MyName";
Console.log (A.name); Undefined

String a special basic data type

In many languages, string is represented as an object, but this tradition is not used in ECMAScript, and string is treated as a basic data type, but it is a relatively special basic type.

It looks as if string should be a reference type, but it's not actually because it's not an object. So it looks like it should be the basic data type and it should be the way the pass value is passed.

Look at the following example:

var stra = "This is a string";
var strb = stra;
   Stra = "This is another string";
Console.log (STRB); This is a string

As we can see in the example above, it is as if stra a copy of the STRB by value. When the stra changed, the STRB did not change, as if we had been able to conclude that string was a basic data type.

However, because a string can be arbitrary in length and passed by value, one copy byte display efficiency remains low, and it seems that a string can also be used as a reference type.

Look at the following example:

var a = "MyObject";
  A.name = "MyName";
Console.log (A.name); Undefined

Display string cannot be treated as an object. In fact, the string in JavaScript cannot be changed, and JavaScript does not provide any method or syntax to change the string.

var a = "MyObject";
  A = a.substring (3,5)
  console.log (a);/BJ

Remember to do this without changing the string string "MyObject", only a refers to another string "BJ", and "MyObject" is recycled.

So you can say that string does not actually fit the above two data type classifications. It is a special type with two properties between them.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.