JavaScript compiler theory of

JavaScript is a compiled language.

JavaScript is compiled within the incident occurred in a few microns (or even less) before the implementation of the code, so JavaScript does not have so much time to optimize other languages.

When the JavaScript execution var a = 2 when we understand not directly create a variable a, and assigned to 2, in fact, it is divided into two steps (This statement well explained upgrade): var a and a = 2;
2 performing var a =, the compiler first break it down into lexical units, then the lexical unit disassembled into an abstract syntax tree (AST).

When JavaScript is compiled, the compiler will ask whether the current scope has been named a variable, and if so, just ignore that statement; otherwise it will declare a new variable in the collection of the scope of the current scope and named a.
After the JavaScript engine run time, it will ask the current scope variables do not have a collection called a, if there is, the JavaScript engine will use this variable, if not, it will continue to look for an upward scope of a variable called . If it finds a, it will give it a value 2. If you have not found, JavaScript engine will throw an exception.

 

Need some attention:
our understanding: function foo (a) {}, it often understood var foo, foo = function (a ) {}, but in fact it is not, the declaration of the function and can not simply LHS assignment in the form of inquiry and understanding.

 

Content Extensions:

  • Abstract syntax tree:

Abstract syntax tree will have a top-level node, called the VariableDeclaration; then there will be a called a child node Identifier, the value of the child node is a; and called the child node AssignmentExpression. The value of the child node AssignmentExpression and there will be a NumericLiteral of the child node is 2.

  • LHS and RHS inquiry

LHS variable container query finds itself;
the RHS query is simply to find the value of a variable.

Guess you like

Origin www.cnblogs.com/Soaring-Free/p/11361056.html