2. JavaScript advanced lexical analysis and javascript lexical analysis

Source: Internet
Author: User

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...

Related Article

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.