Parsing JavaScript, and pre-compiled execution

I. Basic Concepts
Engine: compiled from start to finish and is responsible for program execution javaScript
Compiler: responsible for parsing, code generation
Scope : responsible for collecting and maintaining all declared identifiers (variable) consisting of a series of queries, and implemented a very strict rule, the currently executing code for determining access to this these identifiers
Lexical scoping:   Scope lexical scope and dynamic scope of the points, the scope is javaScript lexical scoping, characterized in that it occurs in the writing process definition phase code will be determined at compile scope objects
Second, before execution:
JavaScrip t is an "interpreted" the language it is also a compiled language, before execution is usually a two-step
1⃣️ parsing: parsing process will construct an abstract syntax tree (AST)
2⃣️ code generation (the compilation phase): AST code generation will be converted into executable code
1. parsing stage:
If the analysis encounters a syntax error, it will throw a syntax error syntax error (syntaxError), stop the execution js code, and then continue to find and load the next block of code; if the syntax is correct, it precompiled into the stage.
2. Compile stages:
 
js operating environment include:
   Global environment (after js code is loaded into the pre-compiler that is, into the global environment)
   Function environment (when the function call to the function into the environment, different functions, different functions environment)
  Each into a different execution environment creates a corresponding execution context, typically a js program creates multiple execution context data structure will js engine stack these to be treated, forming function call stack , the stack is always the bottom global execution context is always top of the stack was the execution context.
 
Creating execution context mainly to do three things:
  1⃣️ create a variable object (variable object)
    Create the arguments object, only in function of the ambient (non-arrow function), the global environment is not this process
    Check the current context of the function declarations, function found earlier statement, if the current variable object does not have the function name attribute is present, the establishment of an attribute it to the function name in the variable object, property values ​​will execute the function where the heap address references, if there is the new reference will be overwritten
    Check the current context of variable declarations, variable declarations found in advance, if the variable object of the current context is not a variable name property, the property to build a variable name in the variable object attribute value undefined; if there is, the variable declaration is ignored
  2⃣️ create scope chain
    (Not before entering into the implementation phase) by the variable objects in the current scope chain execution environment and a range of activities target the upper environment (becomes active after the implementation of the object) composition to ensure that the current execution environment to meet the access to variables and functions order access.
  3⃣️ this point to determine the
Three execution phases:.
  
JS is single-threaded, in order to achieve non-blocking, it uses asynchronous mechanism
  The use of threads:
    1⃣️Js engine threads: the main thread of execution js, go read the main js execution after the completion of the event in the event queue, stack push execution, execution, and so forth called event loop
    2⃣️ event trigger thread (onclick, onload, etc.): attributable to the browser kernel process for controlling the event, the event handler will promote the event queue when an event is triggered
    3⃣️ timer trigger thread, the main thread js deployment, for timing, timing is completed, the thread will be pushed into the event queue callback function
    4⃣️http asynchronous request thread, after XMLHttpRequest connection through the browser to open a new request thread, if there is a callback function that will be requested after the results came back, the callback function to promote the event queue
  Execution order: the macro task (synchronous) - "All micro Tasks -> Macros tasks (asynchronous)
  Task micro browser environment: promise

For example : var A = 2; the console.log (A) ;
concept:
  Assignment of process variables divided into two phases, the first compiler looks at the role of the domain name at compile time if there is a variable, the statement is ignored if found, otherwise it will declare a new variable, just pay attention to the statement, then engine will look at the scope of this variable in the operational phase, if you can find him will be assigned.
There is a concept engine query:
   LHS query, RHS query , the query appears in the LHS variable assignment operator on the left side, that is, "an assignment target" RHS refers to non-left side means "to find the source of an assignment" or "get the value of XXX"
Example:
The first one pairs a LHS is a reference to the second sentence is a RHS a reference
A scope is a set of rules for determining where and how to find variable (identifier). If the goal is to find a variable assignment, it will use  LHS  query; if the goal is to get the value of variables, will use the  RHS  query.
The assignment operator will result  LHS  query. = Incoming operating parameters will result in the assignment associated with the operation when the operator or the scope of the calling function.
JavaScript engine will first be compiled code is executed before, in the process, like var a = 2 such a statement will be broken into two separate steps:
1.  First of all, var A  declare a new variable in its scope. This, is in the beginning stages of compilation phase carried out.
2.  Next, a = 2  queries ( LHS  query) variable  and assign them in the implementation phase carried out.
LHS  and  RHS  inquiry will begin in the current execution scope, if necessary (that is to say they do not find the required identifier), will continue the search identifier to the superior scope, so that every time an increase in scope (floor), and finally arrived in global scope (top level), whether found or did not find the will to stop.
Unsuccessful  RHS  references will cause an  ReferenceError  exception. Unsuccessful  LHS  references will automatically lead to implicitly create a (non-strict mode) a global variable, which is the use of  LHS  target cited as an identifier, or throw  ReferenceError  an exception (in strict mode).
 
 
         

Guess you like

Origin www.cnblogs.com/pianruijie/p/11454598.html