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