Differences between basic types and reference types in javascript

Source: Internet
Author: User

Differences between basic types and reference types in javascript

Basic and reference types

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

The five common basic data types are:

Undifined, Null, Boolean, Number, and String. These five basic data types can directly operate on the actual values stored in the variable.

Take the following example:

var a = 10;var b = a;   b = 20;console.log(a); // 10    var bl = true;var bl1 = bl;   bl1 = false;console.log(bl); // true

Above, B obtains a copy of the value. Although the values of the two variables are equal, the two variables save two different basic data type values. B only saves a copy of. Therefore, when the value of B changes, the value of a is still 10;
Below, the two Boolean variables bl and bl1 are the same basic data types, they also save two different basic data Data Type values, and bl1 stores a copy of bl replication.

Demonstrate the basic data type assignment process:

Let's take a look at the reference type data:

The javascript reference data type is an object stored in the heap memory. Unlike other languages, you cannot directly access the location in the heap memory and operate on the heap memory. You can only use the reference address of the operation object in the stack memory. Therefore, data of the reference type is saved in the stack memory as the reference address of the object in the heap memory. You can use this reference address to quickly find objects stored in heap memory.

See the following example:

Var obj1 = new Object (); var obj2 = obj1; obj2.name = "I have a name"; console. log (obj1.name); // I have a name.

In the above example, we declared a reference data type variable obj1 and assigned it to another reference data type variable obj2. When obj2 adds a name attribute and assigns the value "I have a name ". Obj1 has the same name attribute as obj2. The two referenced data type variables point to the same heap memory object. The obj1 value is assigned to obj2. Actually, the reference address of the heap memory object in the stack memory is copied to obj2, but they all point to the same heap memory object.

The following describes how to assign a value to the referenced data type:

Naturally, adding the name attribute to obj2 actually adds the name attribute to the object in the heap memory. What obj2 and obj1 save in the stack memory is the reference address of the heap memory object, although it is also copied, it points to the same object. Therefore, changing obj2 causes the change of obj1.

Generally, the basic data type is composed of a fixed Number of bytes. These bytes can be operated at a lower layer of the parser, such as Number and Boolean, can contain any number of attributes and elements, so they cannot be as easy to operate as the basic data type. Because the value of the referenced data type changes, passing the same value as the basic data type makes no sense, because it involves a lot of memory replication and comparison, the efficiency is too low. Therefore, the reference data type is passed through the reference transfer method, and the actual transfer is only an address of the object. For example, Array and Function are all reference types because they are special objects. In addition, the reference type can be used to add attributes. Although the basic type can also be used to add attributes, no error is reported, but it cannot be accessed after being added.

See the following code:

var a = 12; a.name = "myname";console.log(a.name); // undefined

String is a special basic data type.

In many languages, String is represented in the form of objects, but this tradition is not used in ECMAScript. String is treated as a basic data type, but it is a special basic type.

It seems that String should be used as a reference type, but in fact it is not because it is not an object. It seems that it should be a basic data type and should be operated by passing values.

Take the following example:

Var stra = "this is a string"; var strb = stra; stra = "this is another string"; console. log (strb); // This is a string

In the above example, we can see that stra copied a copy to strb by passing the value. When stra changed, strb did not change. It seems that we can draw a conclusion that String is a basic data type.

However, because String can be of any length, the display efficiency of one copy byte is still very low through value transfer. It seems that String can also be used as a reference type.

Take the following example:

var a = "myobject";  a.name = "myname";console.log(a.name); // undefined

The display String cannot be processed as an object. In fact, the String in javascript cannot be changed, and javascript does not provide any method and 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 "myobject". Only a references another String "bj" and "myobject" is recycled.

So we can say that String does not actually conform to the preceding two data types. It is a special type with two attributes between two.

The above is all the content of this article. I hope you will like it.

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.