The example analyzes the working principle of the "JavaScript parser" in the browser, and the example analyzes the javascript
When the browser reads HTML files, it will wake up the so-called "JavaScript parser" only when it encounters the <script> tag.
JavaScript parser steps:
1. "Find something": var, function, and parameter (also called pre-resolution)
Note: The following two conditions can be met:
- When the variable and function have the same name, only the function is left
- When the function name is duplicated, the last one is left in the context order of the Code.
2. interpret the code line by line.
Note: The expression can modify the pre-resolution value.
When executing the first pre-resolution step, the JS parser searches for the code from the beginning to the end, and only searches for content such as var, function, and parameter. Generally, the first step is called "JavaScript pre-parsing ". In addition, when the content is found, all variables are assigned a value in advance before the code is officially run: undefined; all functions, before the code is officially run, is the entire function block.
Instance analysis:
Instance 1:
<!DOCTYPE html>
After this code is run, the browser reports an error.
Cause:Because the JavaScript parser does not find any of var, function, and parameters when parsing JS Code, when you execute the code line by line, because a is not found in the "warehouse" and a is not recognized, an error is reported.
Example 2:
<!DOCTYPE html>
After this code is run, the browser will pop up "undefined ".
Cause:Because the "JavaScript parser" finds the var keyword when parsing JS code, and then learns that there is a variable a, a will be assigned an undefined value by default to a and saved to the "repository, therefore, when you execute the code line by line, find the variable a. Because the value of a is undefined, the pop-up value is undefined.
Example 3:
<!DOCTYPE html>
After this code is run, the browser displays the number "1 ".
Cause:Because the "JavaScript parser" finds the var keyword when parsing JS code, and then learns that there is a variable a, a will be assigned an undefined value by default to a and saved to the "repository, when you execute the code line by line, first find the variable a. The value of a is undefined. When you execute the code line var a = 1, a gets a new value "1", so the pop-up value is "1 ".
Example 4:
<!DOCTYPE html>
After this code is run, the pop-up values are in the order of function a () {alert (4);}, 1, 1, 3, and 3.
Cause:Because the "JavaScript parser" first finds the var keyword when parsing the code line by line, and then learns that there is a variable a, a will be assigned an undefined value by default, store it in the "repository", and then continue to parse the code. When function a () {alert (2) ;}is found, the function and variable name are duplicated, keep the function "rule. At this time, the variable changes to function () {alert (2) ;}and continues searching. When the variable a is found, it remains unchanged and continues searching, when function () {alert (4) ;}is found, replace it with function () {alert (4) ;}according to the "function name context" principle );}, in the end, a is assigned function () {alert (4) ;}and saved to the "repository". When the code is executed row by row and the first alert (a) is executed, "function () {alert (4) ;}" will pop up, and then run down. When it is executed to a = 1, because the expression can change the pre-resolution value, therefore, a changes 1. When the second alert (a) is executed, the pop-up value is 1. When the function a () {alert (2) ;}is executed, because it is a function declaration, the value of a is not modified, so when the third alert (a) is executed, the pop-up is still the number "1", and so on, "3" and "3" will pop up one after another ".
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!