Scope and closures (a)

Scope and closures (a)

First, what scope is?

  1, concepts: Design a good set of rules to store variables, and then you can easily find these variables. (And then this value can be accessed and modified.)

  2, the conventional process compiled language: program source code prior to execution, go through the following three steps, referred to as "compiling";

   (1) word / lexical analysis. (* This process will string composed of characters decomposed into (for programming language) meaningful code blocks, the code block is called lexical units.)

      Example: var a = 2; // is decomposed into var, a, =, 2 ,; these lexical units. (Spaces whether as a lexical unit, but depending on its significance in language);

   (2) parsing / syntax analysis.

   (3) code generation.

      Concept: AST into executable code is called code generation. (That is, the var a = 2; AST is converted to a set of machine instructions to create a variable and a value of a * is stored in a medium.)

      Any JavaScript code must be compiled before execution

  3, understand the scope of

    (1) var a = 2; treated three members

      Engine: responsible for compilation and execution process from start to finish the entire JavaScript program.

      Compiler: responsible for parsing and code generation.

      Scope: responsible for collecting and maintaining a series of queries by the identifier (variable) consisting of all life, and to implement a set of very strict rules that determine the code currently executing access to these identifiers.

    (2) compilation process.

      First of all, encountered var a, the compiler will ask whether the scope has a variable with that name exists in the collection of the same scope. If so, the compiler ignores the statement and continue to be compiled; otherwise, it will ask the current scope

         Field set a variable life, and was named a;

      Second, the compiler will generate the code needed to run the engine, the backup code to handle a = 2; this assignment. It will first visit scopes engine is running, if there is a variable called the current scope of the collection. If so, lead

         Engine will use this variable; if not, the engine will continue to look for the variable. (Can not be found, it will throw an exception.)

    Query the left (target LSH, the assignment operation), the right query (the RHS, assignment source operation) (3) for the engine of a; i.e., when the variable appears on the left side of the assignment, for LSH query appears in the right when the side RHS conducted inquiries.

       RHS understood as "get a certain value", LHS trying to find variable container itself, and its assignment.

       Example RHS: console.log (a); a used herein, no assignment, only to find the value of a, and pass the console.log (..)

       LHS example: a = 2; do not care about the current value, just trying to find a target = 2 this assignment.

 

        例: function foo(a){

            console.log(a);

          }

          foo(2);

          Among these are the left side of the query, but also the right of inquiry. There is implicit conversion, a = 2;

  4, nested scopes

       Find variables to start the engine from the current executive role, if not, then continue up to find one. When the global scope, whether or not find, will stop. (RHS query when all nested scopes anywhere to be found less than required

       Variable engine will throw a ReferenceError exception. ) If the "not under strict mode, the left side of the query, finding out that variable, it will create a variable." (Strictly prohibit automatic mode or implicit create a global variable); strict mode

       Under ReferenceError also throw an exception; if the reference value is null or undefined type, and throws a TypeError.

Second, lexical scoping

    To be continued. . . .

  

Guess you like

Origin www.cnblogs.com/yaosusu/p/11407737.html