Javascript pre-compilation and variable scope analysis

Source: Internet
Author: User

Function mm (){}

In this form, a function is declared. Like the mechanism of a variable in Var, the script performs pre-compilation before interpretation and execution.

 

VaR Mm = function (){}

This form assigns a value to a variable. Although it is also pre-compiled, it only allocates a memory space to the MM variable in advance without initialization.

 

Test the following code segments:

 

Code 1:

<SCRIPT type = "text/JavaScript">

Window. Alert (mm );

Function mm (){

 

}

</SCRIPT>

The above code shows the content of mm from alert, but alert is generated before function declaration. It verifies that the Script Host has prepared the script before execution.

 

Code 2:

<SCRIPT type = "text/JavaScript">

Window. Alert (mm );

VaR Mm = 123;

</SCRIPT>

The code above shows that alert has an undefined, indicating that the Script Host has prepared the script before execution: allocates memory space for mm but does not initialize it.

 

Code 3:

<SCRIPT type = "text/JavaScript">

Window. Alert (NN );

Window. Alert (AA );

If (true ){

 

Function mm (){}

VaR AA = 1;

} Else {

 

Function NN (){}

VaR AA = 2;

}

</SCRIPT>

The preceding code re-verifies the pre-compilation and indicates that the pre-compilation has nothing to do with the condition. The NN function definition is displayed first, and then the undefined function is displayed.

 

Note: explain the scope of JS variables.

Javascript variables are also scoped, but they are very general. They are classified into global variables and function variables (local variables)

Variables defined in the function in vitro (in any form) and variables defined by omitting "Var" in the function body are global variables, while variables defined by VAR in the function body are local variables.

Example:

<SCRIPT type = "text/JavaScript">

VaR x = 1; // X1

Function check ()

{

Alert (x); // r1

VaR x = 1; // X2

Alert (x); // r2

}

Check ();

</SCRIPT>

Result: "undefined" is displayed on R1, and "1" is displayed on R2 ".

Reason description:

The X defined by the function in vitro is a global variable. During the pre-compilation process, all the X Statements and function declaration processes are allocated storage units, which is equivalent to a simple record, but there is no JS execution (such as variable initialization ).

During execution, the first X (X1) is initialized and assigned a value. Then, the check () function is called to enter check () and the alert (x) Statement is executed, this is where the JS parser first looks for X in the function body, because the statement of X (X2) in the function body has been recorded in the pre-compilation phase, therefore, the compiler takes the X (X2) defined inside the function body, but X (X2) only declares (the value is "undefined"), So What alert generates is "undefined ". if there is no definition statement for X (X2) in the function body, the JS interpreter cannot find the definition of X in the function body and will look for it at the previous level, in this example, the function is used to look for the definition of X (global variable) in vitro. At this time, the X has been initialized, so alert is 1 at this time.

 

 

 

 

 

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.