Promotion of variable declarations
This is a unique feature of JS, other languages do not have this feature. Some programmers are quite disgusted with this feature.
Let's go ahead and change the value of the variable, and then define the variable, because JS has a mechanism called the elevation of the variable declaration,
JavaScript's variable declarations have a hoisting mechanism, and the JavaScript engine, when executed, promotes the declaration of all variables to the front of the current scope .
So now the program will have seen a row in the program to define the variable before execution, so it will be promoted to the beginning of the program to run
123456789 |
var v = ‘hello‘; (function () {console.log (v); var v = ' World '}) () |
The result is undefined
123456789 |
var v = ‘hello‘; if (true) {Console.log (v); var v = ' World '} |
The output is "hello", which means that JavaScript is not block-scoped . a function is the only structure in JavaScript that has its own scope.
Because JavaScript is a dynamic language, its variables do not have a fixed type, their storage size will vary with initialization and assignment, so the definition of its variable is not like the traditional static language, and its definition does not seem to matter.
Declaring elevation
Declarations within the current scope are promoted to the front of the scope, including declarations of variables and functions
123456 |
(
function
(){
var
a =
"1"
;
var
f =
function
(){};
var
b =
"2"
;
var
c =
"3"
;
})();
|
The declaration of the variable A,F,B,C is promoted to the front of the function scope, similar to the following:
1234567 |
( function () {    var A,F,B,C;    a = "1"    f = function () {};    b = "2"    c = "3" |
Note that the function expression is not promoted, which is also the difference between a function expression and a function declaration. Look further at the difference between the two:
123456789 |
( function () {    //var f1,function F2 () {};//hoisting, implicitly promoted declaration    f1 (); //REFERENCEERROR:F1 is not defined    f2 ();    var f1 = function () {};    function f2 () {} |
The function declaration F2 is promoted in the above code, so it is no problem to call F2 in front. Although the variable F1 is also promoted,
However, the F1 promoted value is undefined, and its true initial value is given at execution to the function expression. So only the declaration is promoted.
Do not write Var
ABC = 123;
Console.log (ABC);//123
When I defined ABC, I did not write Var, and the program did not make an error, indicating that the ABC variable had been successfully defined.
Don't write var defines a global variable that is not controlled by scope
In JavaScript, a name (name) enters the scope (scope) in four ways, with the following order of precedence:
1, language built-in: All scopes have this and arguments keywords
2, formal parameters: function parameters are valid in the scope of the function
3, Function declaration: Shape as function foo () {}
4, Variable declaration: shape as Var bar;
The precedence of the name declaration is shown above, i.e. if the name of a variable is the same as the name of the function, then the name of the function overrides the name of the variable,
Regardless of their order in the code. However, the initialization of a name is done in the order in which it is written in the code, and is not affected by the precedence above. Look at the code:
123456789 |
(
function
(){
var
foo;
console.log(
typeof
foo);
//function
function
foo(){}
foo =
"foo"
;
console.log(
typeof
foo);
//string
})();
|
If there is more than one variable with the same name in the form parameter, the last parameter with the same name overrides the other parameter with the same name, even though the last parameter with the same name is undefined.
There are exceptions to the first name resolution precedence, such as the ability to override the language's built-in name arguments.
Named function expressions
You can specify a name for a function expression like a function declaration, but this does not make the function expression a function declaration. The name of the named function expression does not enter the namespace and is not promoted.
12345 |
f(); //TypeError: f is not a function foo(); //ReferenceError: foo is not defined var f = function foo(){console.log( typeof foo);}; f(); //function foo(); //ReferenceError: foo is not defined |
The name of a named function expression is valid only within the scope of the function.
JS Base Variable Declaration Promotion