Home >Backend Development >PHP Tutorial >ECMAScript primitive and quoted values ​​- JavaScript ING

ECMAScript primitive and quoted values ​​- JavaScript ING

不言
不言Original
2018-03-30 12:44:351904browse

This article mainly shares with you some knowledge about ECMAScript original values ​​and reference values. Friends in need can refer to it

1. The concepts of original values ​​and reference values

In ECMAScript, variables can have two types of values, namely original value and reference value.

1.1 Original value

(1) Original value refers to the value of original type, also called basic type, such as Number, Stirng, Boolean, Null, Underfined.

(2) Simple data segments stored in the stack , that is, their values ​​are stored directly in the locations accessed by variables.

(3) Note

In many languages, strings are regarded as reference types rather than primitive types because the length of the string is variable. ECMAScript breaks this tradition.

1.2 Reference value

(1) Reference value refers to the value of

reference type, such as Object, Function, Array, Date, RegExp.

(2) Objects stored in the

heap , that is to say, the value stored in the variable is a pointer (point), pointing to the storage The memory location of the object.

2. Stack and Heap

2.1 Primitive 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 ECMAScript's

primitive types, namely Undefined, Null, Boolean, Number, and String. Since these primitive types occupy a fixed space , they can be stored in a smaller memory area - the stack. This storage makes it easy to quickly look up the value of the variable.

2.2 Reference values ​​are stored in the heap

If a value is of

reference type, then its storage space will be allocated from the heap. Since the size of the reference value will change , it cannot be placed on the stack, otherwise it will reduce the speed of variable search. Instead, the value placed in the variable's stack space is the address where the object is stored in the heap. The size of the address is fixed, so storing it on the stack does not have any negative impact on variable performance.

2.3 Illustration

ECMAScript primitive and quoted values ​​- JavaScript ING

3. The difference between original value and reference value

3.1 Assignment method

3.2 Whether the value is variable

3.3 Different comparison methods

4. Assignment method and whether the value is variable

4.1 The original value is based on

Assigned in copy way, the value is immutable.

4.2 The reference value is assigned in the

copy way of the reference, and the value is variable.

4.3 Example

ECMAScript primitive and quoted values ​​- JavaScript ING

4.4 Description

(1) Original value assignment→In fact, the value

Copy and assign it to a new variable. This value is copy. It is independent of the original value. Changing one value will not affect the other values.

(2) Reference value assignment → When a reference type, such as an object, uses = to assign it to another variable, it is actually the

address reference of its object that is assigned to the new one. Variables, both variables point to the same address reference, they have the same address. So if you change one of the variables (the object the address reference points to), the other variable (the address reference points to the same object) will also change.

ECMAScript primitive and quoted values ​​- JavaScript ING

4.5 Extension

ECMAScript primitive and quoted values ​​- JavaScript ING

5. Different comparison methods

5.1 The comparison of original values ​​is the comparison of

values

5.2 The comparison of reference values ​​is the comparison of

references

5.3 Example

ECMAScript primitive and quoted values ​​- JavaScript ING

5.4 Description

(1) The original values ​​a and b have different data types, but the values ​​can also be compared. (

Implicit conversion of data types is automatically performed before value comparison)

  • == → Only compare values

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

(2) The reference values ​​obj1 and obj2 respectively refer to two

different values ​​stored in the heap memory Object, so the values ​​(address references) of obj1 and obj2 are different.

read more


1. The concept of original value and reference value

In ECMAScript, variables can have two types of values, namely original value and reference value.

1.1 Original value


(1) Original value refers to the value of original type, also called basic type , such as Number, Stirng, Boolean, Null, Underfined.

(2) Simple data segments stored in the stack , that is, their values ​​are stored directly in the locations accessed by variables.

(3) Note

In many languages, strings are regarded as reference types rather than primitive types because the length of the string is variable. ECMAScript breaks this tradition.

1.2 Reference value

(1) Reference value refers to the value of

reference type, such as Object, Function, Array, Date, RegExp.

(2) Objects stored in the

heap , that is to say, the value stored in the variable is a pointer (point), pointing to the storage The memory location of the object.

2. Stack and Heap

2.1 Primitive 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 ECMAScript's

primitive types, namely Undefined, Null, Boolean, Number, and String. Since these primitive types occupy a fixed space , they can be stored in a smaller memory area - the stack. This storage makes it easy to quickly look up the value of the variable.

2.2 Reference values ​​are stored in the heap

If a value is of

reference type, then its storage space will be allocated from the heap. Since the size of the reference value will change , it cannot be placed on the stack, otherwise it will reduce the speed of variable search. Instead, the value placed in the variable's stack space is the address where the object is stored in the heap. The size of the address is fixed, so storing it on the stack does not have any negative impact on variable performance.

2.3 Illustration

ECMAScript primitive and quoted values ​​- JavaScript ING

3. The difference between original value and reference value

3.1 Assignment method

3.2 Whether the value is variable

3.3 Different comparison methods

4. Assignment method and whether the value is variable

4.1 The original value is based on

Assigned in copy way, the value is immutable.

4.2 The reference value is assigned in the

copy way of the reference, and the value is variable.

4.3 Example

ECMAScript primitive and quoted values ​​- JavaScript ING

4.4 Description

(1) Original value assignment→In fact, the value

Copy and assign it to a new variable. This value is copy. It is independent of the original value. Changing one value will not affect the other values.

(2) Reference value assignment → When a reference type, such as an object, uses = to assign it to another variable, it is actually the

address reference of its object that is assigned to the new one. Variables, both variables point to the same address reference, they have the same address. So if you change one of the variables (the object the address reference points to), the other variable (the address reference points to the same object) will also change.

ECMAScript primitive and quoted values ​​- JavaScript ING

4.5 Extension

ECMAScript primitive and quoted values ​​- JavaScript ING

5. Different comparison methods

5.1 The comparison of original values ​​is the comparison of

values

5.2 The comparison of reference values ​​is the comparison of

references

5.3 Example

ECMAScript primitive and quoted values ​​- JavaScript ING

5.4 Description

(1) The original values ​​a and b have different data types, but the values ​​can also be compared. (

Implicit conversion of data types is automatically performed before value comparison)

  • == → Only compare values

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

(2) The reference values ​​obj1 and obj2 respectively refer to two

different values ​​stored in the heap memory Object, so the values ​​(address references) of obj1 and obj2 are different.

Related recommendations:

Detailed explanation of destructuring assignment of ECMAScript6 variables

The difference between JavaScript and ECMAScript

ECMAScript6 Getting Started with Detailed Examples of Class Objects


The above is the detailed content of ECMAScript primitive and quoted values ​​- JavaScript ING. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn