variables, which are the most basic parts of programming languages, vary from one language to another, but also to a large diameter chamber. Most programming language variables have block-level scopes, such as if, for, and while ... But JavaScript is not pure in block-level scope, but function scope, and has its own unique feature-variable promotion. (ES6 newly added let, const makes it possible to use block-level scopes)
A variable access to a function follows the scope chain, that is, when the current function runs with a current scope, when a variable is consumed, the definition of the variable is found in the current scope, and if it does not exist, the scope of the parent function is looked up according to the scope chain, and then it is used. None continues up until the global scope. About the scope chain is not described in detail here, in simple terms, similar to the prototype chain, from the global function until the scope of the current function has a mutually inclusive relationship, the child can access up, but the parent can not access the variables of the child function, such a layer of nested chain of relationships.
The scope of the action chain is as follows:
var num = 10; function A () {console.log (num); } a (); As a result, there is no num in the scope of a function to look up the outer scope, with and equal to 10 so it pops up 10 instead of undefined.
The elevation of the variable:
var num = 10; function A () { //var num; console.log (num); var num = one; num = one; } A (); Undefined
In this code, the var num = One in function A () {}, the split is equivalent to the commented-out blue part, which is the variable promotion-all variables are promoted to the header declaration of the function scope beforehand.
Take a look at the problem with the function parameter, the first piece of code changes slightly:
var num = 10; function A (num) {console.log (num);
} a (); The result Undefined,a function scope defines the parameter num, because it is not assigned, so it is undefined
Promotion of functions:
There are two ways to declare a function: function declaration and function expression, which can vary in function promotion
function declaration functions are promoted:
Console.log (FN); function fn () {console.log (1);}
function fn () { console.log (1);}
Equivalent to the following section of code:
function fn () {
Console.log (1);
}
Console.log (FN); function fn () {console.log (1);}
In a function declaration, the whole of the declared function is promoted to the top of the scope.
function enhancement for function expressions:
Console.log (FN); UNDEFINEDVAR fn = function () { console.log (1); }
Equivalent to the following section of code:
VAR fn;
Console.log (FN); Undefined
fn = function () {
Console.log (1);
}
function expression, similar to the above mentioned variable promotion, var out of the variable is referred to the top of the scope declaration.
If there are errors found, welcome to shoot Bricks, thank you!
Finally add a classic interview question: http://www.codeceo.com/article/one-javascript-interview.html
JS variable elevation and function promotion