ECMAScript raw values and reference values-JavaScript ING

Source: Internet
Author: User
This article mainly shares with you some ECMAScript primitive values and reference values of knowledge, the need for friends can refer to

First, the concept of primitive values and reference values

in ECMAScript, a variable can have two types of values, the original value and the reference value .

1.1 Original value

(1) The original value refers to the value of the original type , also called the base type , such as number, STIRNG, Boolean, Null, underfined.

(2) Simple data segments stored in stacks (stack) , that is, their values are stored directly in the location where the variable is accessed .

(3) Note

In many languages, strings are treated as reference types, not primitive types, because the length of a string is variable. ECMAScript has broken this tradition.

1.2 Reference value

(1) A reference value refers to a value of a reference type , such as Object, Function, Array, Date, RegExp.

(2) An object stored in the heap (heap) , that is, the value stored at the variable is a pointer (point) to the memory of the storage object .

Second, stack and heap

2.1 Raw values are stored in the stack

When assigning a value to a variable, the ECMASCRIPT interpreter must determine whether the value is a primitive type or a reference type. To do this, the interpreter tries to determine whether the value is one of the original types of ECMAScript, that is, Undefined, Null, Boolean, number, and String. Because the space occupied by These primitive types is fixed, they can be stored in a smaller memory area-the stack . This stores the values that facilitate quick lookup of variables.

2.2 Reference values are stored in the heap

If a value is a reference type , its storage space is allocated from the heap . Because the size of the reference value changes , it cannot be placed in the stack, otherwise it will reduce the speed of the variable search. Instead, the value placed in the stack space of the variable is the address that the object stores in the heap. The size of the address is fixed, so storing it in the stack has no negative effect on the performance of the variable.

2.3 Illustration

Three, the difference between the original value and the reference value

3.1 Ways to assign values

3.2 Value is variable

3.3 Comparison in different ways

Iv. whether the assignment method and value are variable

4.1 The original value is assigned as a copy of the value, and the value is immutable .

4.2 The reference value is assigned as a copy of the reference , and the value is variable .

4.3 Example

4.4 Description

(1) original value assignment → actually copy the value and assign it to a new variable, which is a copy , and the original value is independent of each other, changing one of the values does not affect the other values.

(2) Reference value assignment → when a reference type, such as an object, is assigned to another variable using =, the address reference of its object is actually assigned to the new variable, both of which point to the same address reference, and they have the same address. So if you change one of the variables (the address reference to the object), the other variable (the same object that the address reference points to) will also change.

4.5 Extensions

V. Different ways of comparison

5.1 Comparison of original values is a comparison of values

5.2 Comparison of reference values is a reference comparison

5.3 Example

5.4 Description

(1) raw values A and B have different data types, but you can also compare values. ( implicit conversion of data types is done automatically before value comparison)

    • ==→ only Compare values

    • ===→ not only compare values, but also compare data types

(2) Reference values Obj1 and OBJ2 respectively refer to two different objects stored in heap memory, so obj1 and OBJ2 values (address references) are not the same.

Read more


First, the concept of primitive values and reference values

in ECMAScript, a variable can have two types of values, the original value and the reference value .

1.1 Original value


(1) The original value refers to the value of the original type , also called the base type , such as number, STIRNG, Boolean, Null, underfined.

(2) Simple data segments stored in stacks (stack) , that is, their values are stored directly in the location where the variable is accessed .

(3) Note

In many languages, strings are treated as reference types, not primitive types, because the length of a string is variable. ECMAScript has broken this tradition.

1.2 Reference value

(1) A reference value refers to a value of a reference type , such as Object, Function, Array, Date, RegExp.

(2) An object stored in the heap (heap) , that is, the value stored at the variable is a pointer (point) to the memory of the storage object .

Second, stack and heap

2.1 Raw values are stored in the stack

When assigning a value to a variable, the ECMASCRIPT interpreter must determine whether the value is a primitive type or a reference type. To do this, the interpreter tries to determine whether the value is one of the original types of ECMAScript, that is, Undefined, Null, Boolean, number, and String. Because the space occupied by These primitive types is fixed, they can be stored in a smaller memory area-the stack . This stores the values that facilitate quick lookup of variables.

2.2 Reference values are stored in the heap

If a value is a reference type , its storage space is allocated from the heap . Because the size of the reference value changes , it cannot be placed in the stack, otherwise it will reduce the speed of the variable search. Instead, the value placed in the stack space of the variable is the address that the object stores in the heap. The size of the address is fixed, so storing it in the stack has no negative effect on the performance of the variable.

2.3 Illustration

Three, the difference between the original value and the reference value

3.1 Ways to assign values

3.2 Value is variable

3.3 Comparison in different ways

Iv. whether the assignment method and value are variable

4.1 The original value is assigned as a copy of the value, and the value is immutable .

4.2 The reference value is assigned as a copy of the reference , and the value is variable .

4.3 Example

4.4 Description

(1) original value assignment → actually copy the value and assign it to a new variable, which is a copy , and the original value is independent of each other, changing one of the values does not affect the other values.

(2) Reference value assignment → when a reference type, such as an object, is assigned to another variable using =, the address reference of its object is actually assigned to the new variable, both of which point to the same address reference, and they have the same address. So if you change one of the variables (the address reference to the object), the other variable (the same object that the address reference points to) will also change.

4.5 Extensions

V. Different ways of comparison

5.1 Comparison of original values is a comparison of values

5.2 Comparison of reference values is a reference comparison

5.3 Example

5.4 Description

(1) raw values A and B have different data types, but you can also compare values. ( implicit conversion of data types is done automatically before value comparison)

    • ==→ only Compare values

    • ===→ not only compare values, but also compare data types

The

(2) Reference values Obj1 and Obj2 refer to two different objects that are stored in heap memory respectively, so obj1 and OBJ2 values (address references) are not the same.

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.