Pre-interpretation in JS

Source: Internet
Author: User

1. What is pre-interpretation?

In the current scope, before the JS code executes, it first takes the Var/function keyword (declare) or definition (defined).

2. How does the pre-interpretation work?

A. With the Var keyword, only the advance declaration is completed in the pre-interpretation phase; B. The pre-interpretation phase with the Function keyword completes the declaration and defines two operations.

3, pre-explained the point of attention?

A. The contents of the judging body should be pre-explained regardless of whether the condition is established or not;

For example: if (! (' A ' in window)} {

var a=12;

          Determine if ' A ' is a property name for window, because the pre-interpretation of the declaration of ' a ', so true, take the reverse to false, the condition is not established, a=12 execution, output a=undefined;

}

B. Pre-interpretation of the "=" equal to the left side of the variable is pre-interpretation, the right represents the value, is not pre-interpretation;

Example 1:FN (); //Can be executed in front of the function, since the FN declaration and definition are complete at the time of the pre-interpretation

function fn () {

Console.log (' OK ');

}

Example 2:FN (); //This will cause an error in the FN is not a function, because at the time of the pre-interpretation is only the left side of the equal value is pre-explained, when the value of FN is undefined, not a function, cannot execute 

var fn=function () {

Console.log (' OK ');

}

3, in the global scope of pre-interpretation, the function in the self-executing functions is not involved, the contemporary execution to the corresponding region, declaration, definition, execution together complete;

4, although the function body returns the following code is not executed, but need to do the private scope of the pre-interpretation, and return is the back of the value, is not pre-interpretation;

  For example: Var a=1;

function fn () {

Console.log (a); //Private scope pre-interpretation, first in the private scope Var A, stating that A is a private variable, but return the following code does not execute, so a is undefined;

return function () {}; //Return is not pre-interpreted and returns the corresponding address of the function;

var a=10;

}

FN ();

5, in the pre-interpretation of the time, if the name of a conflict, there is no need to re-declare, but need to re-assign value. (in JS, whether it is a variable or a function, as long as the name is the same, it is conflicting, JS in a name represents a variable, but the stored value can be any data type)

  In the global scope, whether declaring a variable or defining a function, the property name is added to the window.

For example: FN (); //Output result 2

function fn () {console.log (1);}

FN (); //Output result 2

var fn=13;

FN (); //Error

function fn () {console.log (2);}

FN (); //Output result 2

Pre-interpretation in JS

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.