JavaScript precompilation (the principle of variable promotion and function promotion)

Source: Internet
Author: User

Part of this article is transferred from https://www.cnblogs.com/CBDoctor/p/3745246.html

1. Variable Promotion

1Console.log (global);//undefined2 varGlobal = ' Global ';3Console.log (global);//Global4  5 functionfn () {6Console.log (a);//undefined7     varA = ' AAA ';8Console.log (a);//AAA9 }TenFN ();

Question one:

Why did you become a undefined if you haven't defined a and global?

2. Function Promotion

1 // function F1 () {}    2 // undefined   3 function F1 () {} 4 var function () {}

Question two:

Why is Console.log (F1) capable of outputting F1 functions that have not yet been initialized?

Question three:

Similar to question one, why F2 not yet defined, output undefined?

The answers to these questions come from the pre-compilation mechanism of JS:

3. Pre-compilation

JS does not parse execution exactly in code order, but "pre-compile" before parsing. In this process, you will:

(1) Function-first execution of a definition

(2) All var variable definitions, default value is undefined

This explains the above two code output reasons, the above two pieces of code we can use the following form to understand:

Variable elevation:

1 varGlobal;2Console.log (global);//undefined3Global = ' Global ';4Console.log (global);//Global5  6 functionfn () {7       varA;8Console.log (a);//undefined9A = ' AAA ';TenConsole.log (a);//AAA One } AFN ();

function Promotion:

1 function F1 () {} 2 var F2; 3  45function() {}

4. Error-Prone point

1 //Call function, return value 12 f (); 3 functionf () {4Alert (1);5 }6 7 //call the function and return a syntax error. 8 f (); 9 varf =function(){TenAlert (1); One}

This one can understand why, at the pre-compilation stage, the variable f is declared, and it is not assigned a value (anonymous function). Called directly, there must be an error.

5. Summary

JS loading contains both precompilation and execution phases. The compile phase scans all var variables and functions and initializes the VAR variable to the undefined type, while function is initialized to the function value.

At the execution stage, JS is executed sequentially from above, and the Var variable is assigned (therefore, an error occurs before the call is made). When a function variable is encountered, the function is looked up from the active object .

JavaScript precompilation (the principle of variable promotion and function promotion)

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.