1. Principles of compilation

      In the process of traditional compiler language , A piece of source code of a program goes through three steps before it is executed :

* participle / lexical analysis (Tokenizing/Lexing)
        Decomposes a string of characters into meaningful blocks of code , These code blocks are called lexical units .

* analysis / Grammatical analysis
        The lexical unit flow is transformed into a tree which is composed of elements nested level by level, which represents the syntax structure of the program ( Abstract syntax tree )

* code generation
        Converting an abstract syntax tree into an executable process .

2. Understanding scope

    1. Parts involved in code processing :

* engine : Responsible for the compilation and execution of the program from the beginning to the end
* compiler : Responsible for syntax analysis and code generation
* Scope : Responsible for collecting and maintaining a series of queries consisting of all declared identifiers , And implement a very strict set of rules , Determines the access rights of the currently executing code to these identifiers .
    2. The assignment of a variable performs two actions :

* The compiler declares a variable in the current scope
* The engine then looks for the variable in the scope at run time . If it can be found, assign a value to it
    3.LHS Query and RHS query

        The variable appears on the left side of the assignment LHS query : An attempt was made to find the container itself for the variable , So that it can be assigned a value ( That is, who is the target of the assignment operation )

        The variable appears on the right side of the assignment RHS query : Simply look up the value of a variable ( That is, who is the source of assignment operation )

       ( be careful : Parameter passing is actually an implicit assignment operation )

3. Scope nesting

    When a block or function is nested in another block or function , The nested scope occurs .

      therefore , When a variable is not found in the current scope , The engine continues to look in the outer nested scope , Until the variable is found or the outermost scope is reached .

   
 LHS and RHS References are found on the current floor , If not found , You'll take the elevator to the next floor . If you still can't find it , and so on . Once you get to the top floor , It may or may not have been found , In any case, the search process will stop .

4. abnormal

     1. When the variable has not been declared ,LHS and RHS The behavior of the query is different .

* If RHS The query cannot find the required variable in all nested scopes , The engine will throw out ReferenceError abnormal
* If LHS The query also cannot find the target variable at the global scope , The global scope creates a variable with that name , Return it to the engine .( premise : The program runs in non strict mode )
* If RHS A variable was found , But try to manipulate the value of this variable unreasonably , The engine throws a type of exception , be called TypeError.
    2. distinguish ReferenceError and TypeError

* ReferenceError: It is related to the scope of failure discrimination
* TypeError: Indicates that scope discrimination is successful , Yes, it is illegal or unreasonable to operate on the result .
    3. On strict model

        A different behavior of strict mode is : Prevents automatic or implicit creation of global variables .

 

 

Technology