variable, scope

        Before we start, let's clarify a few points of confusion:

1. When the js engine reads the js code, it will perform two steps, ① interpretation ② execution;

2. The explanation is to scan all the js code throughout, bring all the declarations to the top, and the execution is a kind of operation. ;

3. Variable promotion: function declarations and variable declarations are always quietly promoted to the top of the method body by the interpreter;

4. Function hoisting is on top of variable hoisting.

Start:

1. ES (ECMAScript) variables may contain two different types of values: basic type value + reference type value;

2. Basic type values: simple data segments; reference type values: objects that may be composed of multiple values;

3. The value of a reference type is an object stored in memory, and the value of a reference type is accessed by reference.

Say it eloquently:

1. If a variable copies the value of the basic type to another variable, a new value is created on the variable, and then the value is copied to the location allocated for the new variable (copying the value of the basic type);

2. If a variable copies the value of the reference type to another variable, it will also copy the value stored in the variable object and place it at the location where the new variable is allocated, but: the copy of this value is actually just a pointer, and The pointer points to an object stored in the heap. After the copy operation is completed, the two variables actually refer to the same object; therefore, changing one variable will also affect the other variable.

other:

1. js has no block-level scope;

2. Scope: an enclosed space that acts on a certain scope without having any external impact. In this space, the external cannot access internal variables, but internal variables can access external variables.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324871522&siteId=291194637