On the pre-parsing mechanism of JavaScript from the difference between Var func=function and function func ()

Source: Internet
Author: User

The Var func=function and function func () do not have any difference in meaning, but their interpretation precedence is different :
The latter is preceded by other statements at the same statement level.

That

var k =function xx () {return 5;}}


No mistakes,
and

var k =varfunction() {return 5;}}

The error will occur.

Why is that? This begs the pre-parsing mechanism in JavaScript to explain.

The JavaScript parsing process is divided into two stages, one is the compilation phase and the other is the execution phase.

  * Compile Phase

The compile phase is what we often call the JavaScript pre-parsing (preprocessing) phase, in which the JavaScript interpreter will complete the conversion of JavaScript script code to bytecode.

  * Implementation phase

    In the compile-time JavaScript interpreter uses the execution environment to generate mechanical codes for bytecode and executes them sequentially.

What does the compile phase (pre-parsing phase) do?

  * Var, function declaration of variable Promotion

First, create an active object under the current execution environment, then set the variable declared with Var to the active object's property (that is, add it to the active object) and assign it to undefined, and then define the function Also added to the active object.

1 if (false) {2     var aa = 20;3     var bb = 30;4}5 6 function aa () {};7 function bb () {};8 9//var defined AA,BB and function definitions AA (), BB () will be promoted by the variable to the bottom of the Window object

* The difference between function declaration and function expression in pre-parsing

    First, we know that the parser will perform a function declaration promotion (Function declaration hoisting) on the function definition functions (that is, functions declaration) before the code begins to execute, so calling the function before the function declaration does not make an error during execution. But the function expression is different, the function expression is declared with Var, that is, the parser will promote its variable and assign it to undefined, and then, during execution, wait until the VAR variable is executed and then point its variable to a function. Therefore, executing the function before the function expression will result in an error.

1 AA (), 2 function AA () {};3 4 bb (), 5 var bb = function () {};6 7//aa (); no error, because it is a function of the variable promotion, BB () will error, because it is a variable to increase the Var, to its equivalent BB ( ); var BB = undefined; BB = function () {};

 * Function override

    If two functions with the same name are defined, the next one in the pre-parsing period overwrites the signature

1 AA ();   Output I am aa_2;2 function AA () {3    console.log (' I am aa_1 '); 4};5 6 AA ();  Output I am aa_2;7 function AA () {8   console.log (' I am aa_2 '); 9}

  * Pre-parsing an environment in which a variable or function is parsed into its runtime

    The parser does not promote the variable to the Window object, and its promotion is promoted to the environment in which the variable is running.

1 AA = "I am AA"; 2 (function () {3     console.log (AA);  The output AA is undefined 4     var aa = "I am AA in a function"; 5     Console.log (AA);  Output AA is I am AA in a function 6}) (); 7 8//Here a AA is promoted by a  variable, but AA is not promoted to the next window, but is promoted to its running environment (function () {}) (), which is equivalent to 9//AA =  "I am AA"; one//(FU Nction () {//    var aa;  //    Console.log (AA);  The output AA is UNDEFINED14//    AA = "I am AA in a function";    console.log (AA);  Output AA is I am AA in a Function16//}); 17 18 19 20//The following code is equivalent to the above, the purpose is to see below if you do not understand the above. AA = "I am AA"; the function aa () {     console.log (AA), and     the var aa = "I am AA in a function";     console.log (aa );}27 AA ();

* JavaScript "Pre-parsing" segment for

    The so-called segmentation is based on the <script> tag to the pre-analysis of the block

1 <script> 2 AA ();  Output AA2;  3 function AA () {4    console.log (' AA1 '); 5} 6  7 function AA () {8    console.log (' AA2 '); 9}10 </script>11 12 <script>14 function AA () {    console.log (' AA3 ');}17 </script>18 19//The above example shows that the function declaration is chunked, However, the increase of VAR variable is not divided by repeated verification (please advise if there are different opinions here).

  * var variable promotion and function function declaration promotion

    This point is a further explanation of function declaration and function expression, in fact, the preceding function declaration and function expression in the pre-parsed different performance, the main reason is the difference between Var and functions of the promotion. This problem is explained from the parsing phase to the operational phase. First, in the parsing phase after the AA will be promoted and then AA is defined as UNDEFINED,BB will be promoted, and BB is promoted content is the whole function inside the content, in fact, from the browser console we can see one or two. Then, the entire parsing process is over to the running stage, during the run phase, when reading AA (), in fact, the AA variable is pointed to function () {} This function is then called, and to BB (), will be from the previously declared function to find the earlier declared function, Then call directly.

1 var aa = function () {2    console.log (' Aa_ 1 '); 3} 4  5 AA ();//Output aa_1 6 7 8  function AA () {9    Conso Le.log (' aa_2 ');}11 AA (); Output aa_212 13//This example shows that Var is built dynamically during runtime, while function is statically established in the pre-parsing phase.



On the pre-parsing mechanism of JavaScript from the difference between Var func=function and function func ()

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.