First, there is no block-level scope for variables in JS, but there is a function scope, that is, only the function can be the scope of the approximate variable.
And, the essence of the function is also a variable, so you can change its value, that is, assign value. So the variable name promotion is very similar to the function name promotion.
1. The promotion of the variable name occurs inside the function. Look at the following example. Description: The first because of the popup undefined, equivalent to the definition of Var num, because inside the function, the definition of Var num=20, is equivalent to the beginning of the definition of Var num; this is the promotion of the variable name.
var num = ten; function func () { //undefined var num = +; // - } func (); // Ten
Equivalent:
var num = ten; function func () { var num; // undefined var num =; // - } func (); // Ten
If there is no Var
var num = ten; function func () { // num = +; // - } func (); // -
Functions are nested in the same sense
varnum = 10; functionfunc () {alert (num);//undefinednum = 20; functionfunc1 () {alert (num);//undefined varnum = 40; Alert (num)// -} func1 () alert (num); // -} func (); Alert (num)//Ten
2. Function name promotion occurs on a lambda function, which is an anonymous function. and variable names to promote a truth.
var function () { alert ("abc") }; function func1 () { //func is not a function varfunction () { alert ("123") }; Func () } func1 ();
Eliminate Var
var function () { alert ("abc") }; function func1 () { //ABC function () { alert ("123") ) }; Func ()//123 } func1 ();
Change the function without using anonymous functions
var function () { alert ("abc") }; function func1 () { //123 function func () { alert ("123") ) }; Func ()//123 } func1 ();
Variable name promotion and function name promotion in JS