Front-end development-JavaScript lexical analysis

Source: Internet
Author: User

JavaScript code before running a similar compilation process is lexical analysis, lexical analysis is mainly three steps:

1. Parameters of the analysis function

2. Variable declarations for analytic functions

3. function declaration expressions for analytic functions

The steps are as follows:
    • The function generates an active object (active objects) at run-moment, called AO
    • First step: Analyze the parameters:
    1. The function receives the form parameter , which is added to the Ao property, and at this time the value is undefine, i.e. Ao.age=undefine
    2. Receives an argument , adds an attribute to the AO, overwrites the previous undefine
    • The second step: Analysis Variable declaration: such as Var age, or Var age=18;
    1. If the AO has no age attribute in the previous analysis parameter, the AO attribute is added as Undefine, which is ao.age=undefine
    2. If the AO above already has the age attribute, no modification is made
    • The third step: the Declaration of the analysis function:
    • If you have function age () {} to assign the functions to Ao.age, overwrite the values of the previous analysis

Example 1:

function func (age) {    console.log (age);    var age =;    Console.log (age);    Function age () {    }    Console.log (age);} Func (18);

Lexical analysis:

The first step is to analyze the function parameters:

Formal parameter: ao.age = undefined

Actual parameter: Ao.age = 18

The second step is to analyze the local variables:

The 3rd line of code has VAR age, but at this point the first step has ao.age = 18, so do not make any changes

i.e. Ao.age = 18

The third step is to analyze the function declaration:

The 5th line of code has a function of age, then assign function age () {} to ao.age, i.e. Ao.age = function age () {}

So, when executing the code:

The 2nd line of code runs when the age is the lexical analysis of the ao.age, the result is: "function age () {};

The 3rd line of code: 25 is assigned to age, at this time age=25;

The 4th line of code runs when age has been assigned a value of 25, resulting in 25;

  5th, 6 lines of code is a function expression, so no action is done;

The 7th line of code runs when the age is still 25, and the result is 25, running as follows

Lexical analysis should pay attention to the var age = function () {}, this statement, participate in the second and third steps;

When executing code, be aware that the function expression does not do anything, and only declares that the variable is not assigned, and age is still equal to ao.age.

Example 2:

function func (age) {    var;    Console.log (age);    var age =;    Console.log (age);    Function age () {    }    Console.log (age);} Func (18);

Operation Result:

Example 3:

function func (age) {    var;    Console.log (age);    var age =;    Console.log (age);    Function age () {        console.log (age);    }    Age ();    Console.log (age);} Func (18);

Operation Result:

Example 4:

function func (age) {    var;    Console.log (age);    Function age () {        console.log (age);    }    Age ();    Console.log (age);} Func (18);

Operation Result:

Example 5:

function func (age) {    console.log (age);    var = function age () {        console.log (age);    };    Age ();    Console.log (age);} Func (18);

Operation Result:

 

Summary:
The function takes two steps to run: Lexical analysis and statement execution

Lexical analysis:
1. Formal parameters and arguments, actual participation
2. Analyze the variable declaration, such as var age = 18; extract the age=undefined, but do not execute the assignment statement
3. Parse function declaration, such as function age () {} equals var age = function () {}, but the body does not perform

Statement execution:
1.console.log (age);
2.var age = 25;
3.function () {Console.log (age);} is not executed, age () only executes

Precautions:
1. Function declaration Functions age () {console.log;} whole will be assigned to Ao.age
2.var age = function Age () {console.log;} belongs to lexical analysis second step variable declaration

Front-end development-JavaScript lexical analysis

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.