Jjavascript lexical analysis of Python learning Path

Source: Internet
Author: User

Lexical analysis steps

JavaScript has a pre-compiled procedure that is what we call lexical analysis before it runs. The steps of this lexical analysis

    • Analysis parameters
    • Declaration of the Re-analysis variable
    • Analysis Function Description

Liezi:

function func (age) {

Console.log (age); F Age () {}

var age = 27;

Console.log (age); 27

Function age () {}

Console.log (age); 27

  

}

Func (3)

When JavaScript runs, it encounters a function that generates an active object (active objects), followed by an AO

The first step is to analyze the formal parameters:

When the function has formal parameters, it will implement Ao.age = undefined

When we pass in the argument, we put the front overlay ao.age = 3;

Second step analysis of local variables

Find all the local variables and then analyze from the top to the next step.

The above list of var age = 27 begins because of lexical analysis so put ao.age = undefined

So almost all local variables are ao.age = undefined form.

So ao.age = undefined

Because the formal parameter takes precedence over the local variable, ao.age = 3 After the argument is passed in, so here The age attribute value in AO is Ao.age = 3

  Notice here that the value of the formal parameter takes precedence over the local variable in the lexical analysis. The properties of the AO inside

Third Step analysis function declaration expression

After all the local variables have been parsed, we begin to parse the function declaration expressions

The above function will implement Ao.age = function ()

And because it has the same properties, it covers the ao.age = undefined

So the last ao.age = F Age () {}

Note: The above function declares that the highest level of expression overrides the previous same property, but the local and formal parameters do not affect each other.

When we call the function:

The above lexical analysis is done after getting ao.age = F Age () {},

Call function we start to run to the first line of code, let's print, so the first one to print is F age () {}

When you run to var age = 27, the value here is 27.

Next we're going to print the value of age so the second one prints

To the next line is a function expression but this expression does not run, so it does not affect the value of the local variable age.

So the third one prints 27.

This is the above function from the beginning of the lexical analysis, to the last function to run, and then to the back of the function running results.

Here is a code that will explain the above

<Script>    functionfunc (age) {Console.log (age); //3        var Age=  -;    Console.log (age); // -} func (3)        //in this we can look at the age=3 after lexical analysis, which means that formal parameters take precedence over local variables. </Script><Script>    functionfunc (age) {Console.log (age); //F Age () {}        var Age=  -;    Console.log (age); // -        functionAge () {} console.log (age); // -        var Age=  the} func (3)     //above, we can see that when our local variables are behind the function declaration expressions, they do not    //will change the value of this property so that the function declares the highest level of expression, and he will overwrite the previous</Script><Script>    functionfunc () {Console.log (age); //undefined        var Age=  -;    Console.log (age); // -} func ()//There are no formal arguments, no function declaration expressions, so the last age=undefined</Script>

shown in column 1

<Script>    functionfunc (age) {varAge ;    Console.log (age); F Age () {}var Age=  at;     Console.log (age); atfunctionAge () {} console.log (age); } func ( -)</Script>
Start lexical analysis

1. Formal parameters

Start ao.age=undefined

Get Ao.age = 28 after passing in an argument

2. Local parameters

Because there are formal parameters, the local parameter ao.age here does not change. To a.age=28 here.

3. function declaration expression

Here you can get ao.age=f age () {}

Because he has the highest level. So the lexical analysis gets ao.age = F age () {}

function call Execution phase

When executing to the first log: Since there is no assignment for the local variable, this prints the value of the variable in the beginning lexical analysis: F Age () {}

When executing to the second log: The value of the variable in the lexical analysis is overwritten because of the previous local variable assignment so the output is 23

When executing to the third log: No code is executed before, so the output value is 23.

shown in column 2

<Script>    functionfunc (age) {varAge ;     Console.log (age); //F Age () {console.log (age); }        var Age=  at;      Console.log (age); // at        functionAge () {Console.log (age)} age (); //the error says this function is not definedConsole.log (age); //} func ( A)</Script>

Lexical analysis

This is not a detailed analysis of the last can get ao.age = F Age () {}

Execution Phase Analysis

var age: This is just a statement that the local variables do not affect our lexical analysis here.

Console.log (age): So print directly F age () {}

var age = 23: So it covers the age of lexical analysis so here's var age = 23;

Console.log (age): Known by the above age = 23 So here is the 23 printed

This line is a function so do not execute,

Age (): Because we defined the age as a local variable and assigned a value to it, it would be an error to say that this is not a function

Console.log (age): known by the above var age = 23 So the print here is 23

Above we can know that when we define a function, before the function, if you declare a variable with the same name as the function and assign a value, do not overwrite. We can understand that a JavaScript code is a lexical analysis. When we execute a variable assignment in front of the function, the function object we get in the lexical analysis is overwritten by our variable assignment. Because our function is not executed at the time of execution, it is executed only when it is called.

<Script>Console.log (func); //f func (age) {var age;console.log (age), var = 23;console.log (age);}    varfunc= A;    Console.log (func); // A    functionfunc (age) {varAge;console.log (age);var Age=  at; Console.log (age); Console.log (age);} Func ( A)              //the error says there is no this function because the above is covered. </Script><Script>Console.log (func); //f func (age) {var age;console.log (age), var = 23;console.log ( age), Console.log (age);}    functionfunc (age) {varAge ;  Console.log (age); //F Age () {} This comes from lexical analysis        var Age=  at; functionAge () {}//do not executeConsole.log (age); // at} func ( A)              //This is not an error, because none of the resulting lexical analysis has been overwritten. </Script>

The above is a few lexical analysis of JavaScript, which is the way he performs the lexical analysis in advance of the code. The above is my personal morality, some places may not be very clear. There's no way to limit it.

Jjavascript lexical analysis of Python learning Path

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.