When the browser reads the HTML file, it only wakes up the so-called "JavaScript parser" when it encounters the <script> tag.
JavaScript parser work steps
1. "Find something": Var, function, Parameters: (also referred to as pre-parsing)
Note: If a duplicate name is encountered in the following two cases:
(1) The names of variables and functions are encountered, leaving only the function
(2) The name of the function is encountered, according to the context order of the code, leaving the last
2. Line-wise interpretation of code
Note: An expression can modify a pre-resolved value
JS parser in the first step of the pre-parsing, from the beginning of the code to search until the end, only to find the Var, function and parameters and so on, generally the first step is called "pre-parsing of JavaScript", and when the content is found, all the variables, Before formally running the code, a value was assigned in advance: undefined (undefined), all functions, before the code is formally run, is the entire function block
Example 1
<! DOCTYPE html>"en">" UTF-8"> <title>Document</title> <script type="text/ JavaScript"> Alert (a); </script>
After this code is run, the browser will error.
Reason: Because, "javascript parser" in parsing JS code, no Var, function, parameters, and so on any one of them, so when the code is executed row by line, because in the "warehouse" can not find a, do not know A, will be an error
Example 2
<! DOCTYPE html>"en">" UTF-8"> <title>Document</title> <script type="text/ JavaScript"> Alert (a); var 1 ; </script>
After this code is run, the browser pops up "undefined".
Reason: Because, "javascript parser" in parsing JS code, find the var keyword, and then know that there is a variable a, so will give a default assignment of a undefined value, stored in the "warehouse", so when the line of code execution, find the variable A, Because the value of a at this point is undefined, the value that pops up is undefined.
Example 3
<! DOCTYPE html>"en">" UTF-8"> <title>Document</title> <script type="text/ JavaScript"> var1; alert (a); </script>
After this code runs, the browser will pop up the number "1".
Reason: Because, "javascript parser" in parsing JS code, find the var keyword, and then know that there is a variable a, so will give a default assignment of a undefined value, in the "warehouse", in line execution code, first find the variable A, At this point the value of a is undefined, when executed to the Var a= 1 line of code, a gets a new assignment "1" so the value of the popup is the number "1".
<! DOCTYPE html>"en">"UTF-8"> <title>Document</title> <script type="Text/javascript">alert (a); varA =1; alert (a); function A () {alert (2); } alert (a); varA =3; alert (a); function A () {alert (4); } alert (a); </script>After this code is run, the order of the popup values is: function A () {alert (4);}, 1, 1, 3, 3
Cause: Because the JavaScript parser finds the VAR keyword in line-by-row parsing of the code, and then learns that there is a variable a, it assigns a undefined value to a default, deposits it in the "warehouse", and then continues to parse the code down when it finds function a () { Alert (2);} , the "function and variable name, reserved function" rule, at this point a becomes function () {alert (2),}, and then continue to look down, when the variable A is found, the same, still continue to look down, when found function () {alert (4);} , it is replaced with function () {alert (4), which is assigned as function () {alert (4), and in "warehouse", when executed to the first alert (a) when the code is executed line by row, the " function () {alert (4);} ", then execution down, when executed to a=1, because the expression can change the pre-parsed value, so at this point a becomes 1, executes to the second alert (a), the pop-up value is 1 when executed to function a () {alert (2) ;} , because this is a function declaration, and does not modify the value of a, so when the third alert (a) is executed, the pop-up is still the number "1", and so on, and thereafter will be ejected "3", "3".
Transferred from: http://www.cnblogs.com/webhome/p/6164245.html
JavaScript parser principle