2. JavaScript advanced lexical analysis and javascript lexical analysis
JavaScript code is executed from top to bottom. However, lexical analysis is performed before JavaScript code is executed.Lexical AnalysisAndRunTwo stages.
Lexical Analysis
Lexical analysis is mainly divided into three steps:
Step 1: analyze the parameters
Step 2: analyze the variable Declaration
Step 2: analyze the function declaration
If function nesting exists, lexical analysis is performed from outside to inside.
Procedure:
0: Generate Active Object (activity Object) at the moment when the function is executed.
1:
1.1 form parameters declared by the function form the attributes of AO. The default value is undefined,
1.2 receive the form parameter and assign a value to the form parameter that has just formed the AO attribute
2: analyze the var declaration variable! Such as var age; (the variable value is determined during the running period)
2.1 if there is no age attribute on AO, add the age attribute to AO. The default value is undefined.
2.2 If the age attribute already exists on AO, no operation is performed.
3: analysis function declaration! For example, function foot (){}
3.1 if there is no foot attribute on AO, the function is assigned to the AO. foot attribute
3.2 If the AO has the foot attribute, it will overwrite it directly and assign the function to the AO. foot attribute.
Code demonstration and analysis:
function a(b){ alert(b); function b(){ alert(b); } b(); } a(1);
This is a common JavaScript interview question. If you do not understand the lexical analysis of JavaScript, you cannot understand it at all. Next we will analyze the lexical steps of JavaScript. We have mentioned that JavaScript is executed from top to bottom, however, after lexical analysis, the code is executed.
Analysis process:
0. Forming activity object AO = {}
1. Analyze the parameter, --> AO = {B: undefined}; analyze the passing parameter, --> AO = {B: 1}
2. Analysis variable declaration var, no
3. Analyze the function declaration, AO. B = function () {alert (B) ;}, and perform the overwrite operation.
Execution Process:
Alert (B); // function
B (); // Execute function B... alert (B), within the scope of function B can not find B, according to the scope chain principle (see the previous http://blog.csdn.net/guixuecheng/article/details/43670323) to find the outer, find B is the function itself, print out the function...