JavaScript reference types and basic types _ javascript skills

Source: Internet
Author: User
This article mainly introduces the reference types and basic types of JavaScript. For more information, see the following two types of data in javascript: Basic Types and reference types.

The basic type is simply a simple data segment.

A reference type is an object composed of multiple values.

When assigning values, the parser first analyzes whether the data is of the value type or reference type.

Two access methods:

Basic Type value: access by value and operate on the actually saved values;

Reference Type value: access by reference. When querying, We need to read the memory address from the stack first, and then find the value stored in the heap memory;

The following describes the two types of javascript data.

I. Basic data types:

There are six basic types of data in javascript: string, number, boolean, symbol (New in ES6), null, undefined.
These five basic data types can directly operate on the actual values stored in the variable.

The code example is as follows:

var a=10;var b=a;b=20;console.log(a);

The above code is a simple assignment operation. The following is a brief introduction.

(1). First, the number is the basic data type.
(2). var B = a. This assignment operation is to copy the data of a and assign the value to variable B.
(3). a and B are completely independent.
(4). B = 20. Modifying the value of variable B does not affect the value of variable.

The figure is as follows:

Stack memory


Ii. reference type data:

In javascript, reference data is stored in the heap memory, but you cannot directly access the location in the heap memory space and operate on the heap memory space.
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.

Code example:

Var obj1 = new Object (); var obj2 = obj1; obj2.name = ""; console. log (obj1.name );

Next, let's take a look at the code above the code.

(1). var obj1 = new Object (), which creates an Object and a reference type data. The variable obj1 stores the address of the Object in the heap memory.
(2 ). var obj2 = obj1. The value assignment operation is to copy the storage address of the object in the heap memory to the variable obj2, that is, the two variables store the address pointing to the actual memory address of the object, it points to the same object.
(3). obj2.name = "" to add an attribute to the object.
(4). console. log (obj1.name), output "script home, because the two variables point to the same object.

The figure is as follows:


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.

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.