The code for processing three roles
Engine: JavaScript programs from start to finish responsible for the entire process of compiling and executing
the compiler: responsible for parsing and code generation
scopes: responsible for collecting and maintaining all the variables of inquiry
There are a = 2;
The compiler of this procedure will first be decomposed into lexical units, and the stream analysis unit into a lexical tree. The tree structure is then converted to executable code, i.e. a computer to understand instructions. Memory assigned to a variable, named as a, then the value saved to the variable 2. This is consistent with compiler theory
But not entirely correct
In fact the compiler will be handled as follows
1, lexical analysis, encountered var A, the compiler will ask whether there are current scope of the variables. That ignores the variables continue to compile. No, it requires a new variable scope declaration 2, in the generated code, the compiler of a = 2 for assignment. First ask scope, the scope of the presence of the variable current. That the use of the variable. No, the engine continues to look for the variable
If the engine found a, 2 will be assigned to it. Otherwise, an exception is thrown
Variable assignment performs two actions, a compiler declare the variables in the current scope, then engine to find the variable in scope, assignment
Search process conducted by the scope of the assistance, but how to perform the search engine, search results will affect the final. LHS query engine will be as variable a. Also look for a type called RHS. They represent the assignment of the left and right operation. More precisely, LHS inquiry is trying to find the variable container itself.
LHS and RHS means "left or right side of an assignment" does not necessarily mean that "assignment = left or right side of the operator." There are several other forms of assignment, so the concept is best understood as to "who is the target of an assignment (LHS)" and "Who is the source of an assignment (RHS)".
such as
function foo(a) { console.log(a) //2 } foo(2);
RHS to be foo references. LHS to be a reference. RHS of the console were cited
function foo(a){ var b = a; return a + b; } var c = foo(2);
Here there is the LHS 3, RHS has 4
Every look here, we need to meet with a scope