Example analysis in the browser "JavaScript parser" working principle _javascript skills

Source: Internet
Author: User

When the browser reads the HTML file, it wakes up the so-called "JavaScript parser" when it encounters the <script> tag.

JavaScript Parser Work Step:

1, "Find something": Var, function, parameters; (also known as pre-parsing)

Notes : If you encounter a duplicate name, it is divided into the following two situations:

    • The name of the variable and function is encountered, leaving only the function
    • The name of the function is encountered, leaving the last one, depending on the context order of the Code

2. Read code by line.

Note: An expression can modify a pre resolved value

When the JS parser performs the first step of parsing, it searches from the beginning of the code until the end, only to find the contents of Var, function and parameters. The first step is generally referred to as "JavaScript's pre-parsing." And, when this content is found, all variables are assigned a value in advance before the code is formally run: undefined; all functions are the entire block of functions before the code is formally run.

Example Analysis:

Example one:

<! DOCTYPE html>
  
 

After this code is run, the browser will complain.

Cause: because the JavaScript parser does not find any of the Var, function, parameters, and so on when parsing the JS code, the error occurs when the code is executed on a line-by-row basis because a is not found in the warehouse and a is not known.

Example two:

<! DOCTYPE html>
  
 

After this code is run, the browser pops up "undefined".

reason: because, "javascript parser" in parsing JS code, find the var keyword, and then learned that there is a variable a, so will give a default assignment of a undefined value, into the "warehouse", so, when the line-by-row execution code, find variable A, Because the value of a at this time is undefined, the pop-up value is undefined.

Example three:

<! DOCTYPE html>
 
 

After this code is run, the browser pops up the number "1".

reason: because, "javascript parser" in parsing JS code, find the var keyword, and then learned that there is a variable a, so will give a default assignment of a undefined value, stored in the "warehouse", the line-by-row execution of code, first find the variable A, At this point, the value of a is undefined, and when the line code is executed to Var a= 1, a gets a new assignment "1" so the value that pops up is the number "1".

Example four:

<! DOCTYPE html>
 
 

After this code runs, the order of the pop-up values is: function A () {alert (4);}, 1, 1, 3, 3

reason: because the "JavaScript parser" parses the code line-by-row, it finds the Var keyword and then learns that there is a variable a, so it assigns a undefined value to a by default, stores it in the warehouse, and then continues parsing the code down, When function A () {alert (2) is found , depending on the duplicate name of the function and variable, retains the rule of the function ", at which point a becomes function () {alert (2);}, and then continue looking down, and when the variable A is found, it continues to look down when the function function () {alert (4) is found.} , according to the "function with the same name context" principle, replace with function () {alert (4);}, final A is assigned function () {alert (4);}, stored in "warehouse", when executing code line by row, the first alert (a) will pop up " function () {alert (4); ", and then down, and when you execute to a=1, because the expression can change the resolved value, then a becomes 1, and when you execute the second alert (a), the pop-up value is 1, when you execute to function a () {alert (2) ;} , because this is a function declaration and will not modify the value of a, so execution to the third alert (a), the pop-up is still the number "1", and so on, and so on, then will be pop-up "3", "3".

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.