Summary of JS Pre-parsing problem

Source: Internet
Author: User

// Example 1alert (a)       //  undefind. alert (FN)      //  function whole block.  var a = 1; function fn () {     return  falss;};

Why? The first is undefind that the second is a functin function block. How is this going to work?

The JS parser is divided into two steps when running the JS code:

    1. The first step is to move all 变量 , 函数 参数 forward, to the top of the current scope.
    2. The second step is to interpret the code by line 从左到右 从上至下 .
//Example 1 explanationvarA//The a variable is advanced to the front of the entire scope before the current value is undefind. functionfn () {return false;}; //The FN function precedes the entire scope before the current value is the entire function block. alert (a); //Undefind. Alert (FN);//function the entire block of functions. A=1;//a assignment 1 now has value for a variable: 1functionfn () {return false;}; //The function has not changed but was preceded by the pre-scope. I remember saying something like this:                    //The function in that definition is not important to call on that.                     //(because it will be preceded by the current scope after the definition, in which scope the call takes effect)

Tips: The parsing engine performs the creation of all Var variables in block-level , giving an initial value of undefined.

alert ( a);           // a function block.  var a = 1; alert (a)          //  a assigns a value equal to 1.  function  A () {    returnfalse;}
//Example 3 explanationvarA//first, the current value of the advance variable is: Undefind. functionA () {return false;}; //Encounter function Advance function Note: The current function is the same as the above variable name, how to do?                         //don't worry. The JS parser has already done a function with the same name will replace the same name of the variable now only left a function block itself. alert (a); //because only a function is left above, the entire function block of the A function is now ejected. A = 1;//A = 1 expression is assigned so the A function is replaced with the variable value: 1. alert (a);//the value that pops up now is a value of 1 that has been changed;functionA () {//This function is useless now.     return false;};

Pre-parse with parameters: notice there's a scope problem here.

// Example 4 var a = 1; function Fn1 (a) {    alert (a);     = 2;} ; Fn1 ();                                 // Undefind. alert (a);                             // A value is: 1. 
//Example 4 explanationvarA//A variable advance current value: Undefind. (Global variables)functionFn1 (a) {alert (a); A= 2;} ;//FN1 function Block Advance value is: entire function                                      //The advance is all in advance and now begins to interpret the code line by row,                                      //wait, somebody's going to say the parameters? Hahaha don't worry about it yet. varA = 1;//A = 1 An assignment expression changes the value of a variable A to: 1. functionFn1 (a) {varA//parameters are parsed as variables to the top of the current function. alert (a);//a value in the current function is: undefined.A = 2;//A = 2 copy expression is useless};//function blocks do not experience calls that are useless. FN1 ();//Now that we have a call to function fn1, let's go inside and see.                                       //after the call succeeds, the Undefind is bounced. alert (a);//This is still called global variable A with a value of 1. 

Summary of JS Pre-parsing problem

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.