js data type, stack storage, multi-data type calculation

js data type, stack storage, multi-data type calculation

What are the js data types

Basic data types (value types): Number, String, Boolean, Undefined, Null, Symbol (new unique values ​​in es6) and BigInt (new in es10);

Reference data type: Object. Including Object, Array, function, Date, RegExp.

Remarks: Basic data type, also known as value type.

Stack storage

Value type stack storage: mainly for three types of data (Number, String, Boolean). Stored directly in the stack, occupying a small space and a fixed size, is frequently used data, so it is stored in the stack.
E.g:
The change in stack memory when we execute the following code:

var a=100;

var b=100;

a=200;

Insert picture description here

Reference type stack storage: mainly for Object and Array reference data and null, which are stored in the stack and heap at the same time, occupying a large space and the size is not fixed. The reference data type stores a pointer in the stack, which points to the starting address of the entity in the heap. When the interpreter looks for a reference value, it first retrieves its address on the stack, and then obtains the entity from the heap after obtaining the address.

Insert picture description here

Remarks:

Reference type storage stores the value in the heap memory, and the heap memory is stored from bottom to top. Generate a unique memory address. Then assign the address to the variable in the stack memory. The stack memory is stored from top to bottom. The reason for dividing the memory in this way mainly considers the efficiency of value transfer for particularly large objects.

Common value types:

Insert picture description here

Common reference types:

Note: Because typeof detects null and returns object, some places also become null as a special reference type. But we are better classified as basic types.
Insert picture description here

typeof operator:

Basic type (value type):

Insert picture description here

to sum up:

typeof can effectively detect basic types, and all reference types will return object, where null belongs to a special reference type and returns object, function belongs to a special reference type and is not used to store data, and typeof detection returns function.

Variable calculation-type conversion

Insert picture description here

1. String splicing

const a=100+200; // 200
const b=100+'200' // '100200'
const c='true'+'100' // 'true100'

Conclusion: There are strings in the + operation, and the result is string concatenation.

2.==与===

Insert picture description here
Insert picture description here

in conclusion:

===Third-class means congruence, judging whether the objects or values ​​on the left and right sides are of the same type and value.
==Second-class means that the values ​​are equal. Judging whether the objects or values ​​on both sides of the operator are equal and the types can be different. When the types are different, use the Number()conversion to Numbertype for judgment. Exception rule, null==undefined,null/undefinedno implicit type conversion is performed when performing operations. Usually the value is converted to a Boolean value for conditional judgment. Boolean(null)===Boolean(undefined)>false===false The result is true

3. If statement and logical operations

truly variable and fasely

Insert picture description here

Common falsely variables (other than true variables).

Insert picture description here

to sum up:

Of all the basic types, there are only 6 Boolean values ​​that are false. They are: 0 NaN '' null undefined false
Reference type Boolean values ​​are all true.

Judgement in if statement

Insert picture description here

to sum up:

When the if condition is a single value, if it is a true value, the condition is established, if it is a false value, the condition is not established

Logical judgment:

Insert picture description here

to sum up:

Logical AND (&&) operation rules: The left side is truly returning to the right side, and the left side is fasely returning to the left side.
Logical OR (| |) operation rules: Truley returns to the left on the left, and fasely returns to the right on the left.

手写深拷贝
Because the reference type uses address passing, another deep copy of the problem is generated, see another blog for details. Handwritten deep copy

Guess you like

Origin blog.csdn.net/WLIULIANBO/article/details/110672033