JS's variable elevation and function promotion

Source: Internet
Author: User

The original said is in-depth understanding, but the writing is very simple, 233, the original link: http://www.cnblogs.com/kawask/p/6225317.html, I added a little explanation here, so it is not marked as a purely reproduced article, 233

--------------------------------------------------------------------------------------------------------------- -

First, variable promotion

Before ES6, JavaScript had no block-level scope (a pair of curly braces {} is a block-level scope), with only global scope and function scope. The variable elevation is about to elevate the variable declaration to the beginning of the scope where it is located. Examples of previous resumes are:

// undefined var global = ' global '//  globalfunction//  Undefinedvar a = ' aaa '//  AAA}fn ();  

The reason is that the above printing results, is due to JS variable promotion, in fact, the above code is executed according to the following:

var // variable promotion, global scope, at this time only declared, and not assigned // undefined // The value is now assigned // Print out Global function  fn () {  var//  variable promotion, function  scope = ' aaa '; Console.log (a);} fn ();

Second, function promotion

There are two ways to create functions in JS: function declarations and function literals. function promotion only exists for function declarations! Such as:

// function F1 () {}    // undefined   function F1 () {} var function () {}

Only so will have the above print result, is because the function promotion in JS causes the code to actually execute according to the following:

function // function Promotion, the entire code block is promoted to the beginning of the file <br> Console.log (   F1); Console.log (F2);    var function () {}

Conclusion: Basically this is the case, to master the words can do more exercises, test:

Console.log (F1 ()); Console.log (F2);    function F1 () {console.log (' AA ')}varfunction() {}
(function= ' aaa ';   var a = ' BBB '; Console.log (a);}) ();

The original text did not give the result, I explained here again:

The first Test,

Aa
Undefined
Undefined

Because the function declares the promotion, the third line mentions the first line, and runs the first output AA, but neither of these functions returns a value, of course undefined.

A second Test,

Undefined
Bbb

This is a closure, but it's no use here, the order of execution or

(function() {    var= ' aaa ';    = ' BBB '; Console.log (a);}) ();

JS's variable elevation and function promotion

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.