-# #1. What is a scope # #
Any programming language can store variables and modify the values of variables, but how to introduce variables is an issue where variables are stored where to find variables.
To better let us find where these variables are stored, and how to get the program to find variables, we create a series of rules that we call scopes;
# # #1.1 Compiling principle # # #
Each code goes through three steps before it executes.
1. Word Segmentation/Lexical analysis
2. Analytic/syntactic analysis
3. Code generation
* * Word segmentation/Lexical analysis * *
This process breaks down the character string code into meaningful blocks of code that we call lexical units, such as:
var a = 2;
This code will be decomposed into Var, a, =, 2,; Whether a space can be treated as a lexical unit depends on the space's meaning in the code;
* * Parsing/syntax unit * *
The process is to change the set of lexical units into a tree of grammatical rules that consists of nesting elements (we call this tree abstract syntax tree, or AST), for example
Var
/
X
/
=
/
2
* * Code Generation * *
The process is to transform the tree we parse into executable code, throw away the different languages and different platforms, simply, the var a = 2 of the abstract syntax tree into a machine-readable instruction, let the machine create a variable, and assign a value of 2;
* * Key points * *
Unlike other languages, JS code does not take a lot of time to optimize, because JS compilation is not executed in advance, most of the time the code is compiled in the code before the execution of a very small period of time (a few microseconds or even less time), so any JS code will be compiled before execution, And will usually be executed immediately after compilation.
JS Scope 1