Article Original address: https://www.cnblogs.com/zingp/p/6102561.html
JavaScript code before running a similar compilation process is lexical analysis, lexical analysis is mainly three steps:
- Analysis parameters
- Declaration of the Re-analysis variable
- Analysis function declaration
The steps are as follows:
- The function generates an active object (active objects) at run-moment, called AO
- First step: Analyze the parameters:
- The function receives the form parameter, which is added to the Ao property, and at this time the value is undefine, i.e. Ao.age=undefine
- Receives an argument, adds an attribute to the AO, overwrites the previous undefine
- The second step: Analysis Variable declaration: such as Var age, or Var age=18;
- If the AO has no age attribute in the previous analysis parameter, the AO attribute is added as Undefine, which is ao.age=undefine
- If the AO above already has the age attribute, no modification is made
- The third step: the Declaration of the analysis function:
- If you have function age () {} to assign the functions to Ao.age, overwrite the values of the previous analysis
See a code to practice practiced hand:
1 function func (age) {2 Console.log (age), 3 var = 4 Console.log (age), 5 function age () {6 } 7 Console.log (age); 8 9}10 func (18);
Lexical analysis:
The first step is to analyze the function parameters:
Formal parameters: Ao.age = undefined
Actual parameter: Ao.age = 18
The second step is to analyze the local variables:
The 3rd line of code has VAR age, but at this point the first step has ao.age = 18, so do not make any changes
i.e. Ao.age = 18
The third step is to analyze the function declaration:
The 5th line of code has the function age, which is paid to Ao.age, which is ao.age = function age () {}
So, when executing the code:
The 2nd line of code runs when the age is the lexical analysis of the ao.age, the result is: "function age () {};
The 3rd line of code: 25 is assigned to age, at this time age=25;
The 4th line of code runs when age has been assigned a value of 25, resulting in 25;
5th, 6 lines of code is a function expression, so no action is done;
The 7th line of code runs when age is still 25, and the result is 25. Look at the results of the browser execution, bingo~~
Lexical analysis should pay attention to the var age = function () {}, this statement, participate in the second and third steps;
When executing code, be aware that the function expression does not do anything, and only declares that the variable is not assigned, and age is still equal to ao.age.
Example 2:
1 function func (age) {2 var, 3 Console.log (age), 4 var age = + 5 console.log (age), 6 function A GE () {7 } 8 Console.log (age); 9}11 func (18);
Answer:
Example 3:
1 function func (age) {2 var, 3 Console.log (age), 4 var age = + 5 console.log (age), 6 funct Ion Age () {7 Console.log (age), 8 } 9 Age (), console.log (age),}13 func (18);
Answer:
Example 4:
1 function func (age) {2 var, 3 Console.log (age), 4 function age () {5 console.log (age); 6 } 7
age (); 8 Console.log (age); 9}11 func (18);
Answer:
Example 5:
1 function func (age) {2 Console.log (age), 3 var = function age () {4 Console.log (age), 5 };6 Age (), 7 Console.log (age), 8}9 func (18);
On the procedure of JavaScript lexical analysis