Analysis of the difference between basic types and reference types in JavaScript

Source: Internet
Author: User

Most of the programming languages that most people learn systematically are the differences between value types and reference types, one of the first points learned in the learning process of these languages. Let's look at the differences between basic data types (Primitive Types) and reference types (Reference Types) in JavaScript. 、

Basic types and reference types

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

The five basic data types that are common are:

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

Look at the following example:

?
123456789 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 the value of a value, although the value of two variables is equal, but two variables hold two different base data type values. b Just a copy of a copy is saved. So, when the value of B changes, the value of a is still 10;
Below, the two Boolean variables bl and BL1 are also basic data types, and the same holds two different base data type values, BL1 saves a copy of the BL copy.

Demonstrates the process of assigning this basic data type:

Here's a look at reference type data:

The JavaScript reference data type is an object stored in the heap memory, and unlike other languages, you cannot directly access the location in the heap memory space and manipulate the heap memory space. Only by manipulating the object's reference address in the stack memory. So referencing the type of data that is stored in the stack memory is actually the object's reference address in the heap memory. This reference address allows you to quickly find objects that are stored in heap memory.

Take a look at the following example:

?
1234 varobj1 = new Object();varobj2 = obj1;obj2.name = "我有名字了";console.log(obj1.name); // 我有名字了

From the above example, we declare a reference data type variable obj1 and assign it to another reference data type variable OBJ2. When we obj2 add a name attribute and assign the value "I have a name". OBJ1 also has the same name attribute as the OBJ2. Describes the two reference data type variables that point to the same heap memory object. Obj1 assignment to Obj2, actually just copy the heap memory object at the reference address of the stack memory to the OBJ2, but they essentially point to the same heap memory object.

Let's demonstrate this reference data type assignment process:

Naturally, adding the Name property to Obj2 is actually adding the Name property to the objects in the heap memory, and Obj2 and obj1 are only the reference addresses of the heap memory objects that are stored in the stack memory, although they are also copied, but the object is the same. So the change of OBJ2 caused the obj1 change.

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

Look at the following code:

?
123 vara = 12; a.name = "myname";console.log(a.name); // undefined

String a special basic data type

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

It looks as if a string should be a reference type, but it is not, because it is not an object. Then it looks like it should be the basic data type and should be manipulated by means of a pass-through value.

Look at the following example:

?
1234 varstra = "这是一个字符串";varstrb = stra; stra = "这是另外一个字符串";console.log(strb); // 这是一个字符串

As we can see from the above example, it is as if Stra copied a copy to STRB by means of a value transfer. When Stra changes, STRB does not change, and it seems we can already conclude that string is a basic data type.

However, because a string is arbitrary in length and is passed by value, one of the copied bytes is still very inefficient and appears to be a reference type for string.

Look at the following example:

?
123 vara = "myobject"; a.name = "myname";console.log(a.name); // undefined

Shows that a 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.

?
123 vara = "myobject"; a = a.substring(3,5) console.log(a); // bj

Remember to do so, there is no change to the string string "MyObject", only a refers to another string "BJ", "MyObject" is recycled.

So it can be said that string does not actually conform to the above two types of data classification. It is a special type that has two attributes that are between two.

The above mentioned is the whole content of this article, I hope you can like.

Analysis of the difference between basic types and reference types in JavaScript

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.