JavaScript uses the lexical scope of the working model.
Defined
Lexical: Most standard language compiler's first working stage is called lexical (word), this process will check the characters in the source code, if it is stateful parsing process, but also give meaning to the word.
Lexical scope: Defines the scope in the lexical phase .
The lexical scope is determined by WHO: where you write the variables and block scopes When you write the code. Therefore, in most cases, the lexical analyzer retains its effect when it processes the code.
Cases
function foo (a) { var2; function Bar (c) { Console.log (A, B, c); } 3 2 );
Output: 2,4,12.
Parsing: There are three nested scopes in this example, such as:
① contains the entire global scope, where there is only one identifier: foo
② contains a scope created by Foo, with three identifiers: A,b,bar
The ③ contains the scope created by bar and has an identifier: C
Scopes are determined by where the corresponding scope block code is written, and they are included hierarchically.
Note: There are no scopes that can occur simultaneously or partially in two external scopes.
A scope lookup stops when the first matching identifier is found.
"You don't know JavaScript" reading notes (II.) lexical scopes