Javascript variables and their scopes and early declarations, javascript Variables
Variables and their scopes in javascript are the key points in the foundation. After reading the JavaScript authoritative guide, we will summarize:
If the initial value is not specified for the variable in the var declaration statement, although this variable is declared, its initial value is undefined before it is saved to a value.
(If a variable is declared in a scope, it is also undefined before the value is assigned)
The function scope of JavaScript indicates that all variables declared within the function are always visible in the function body. Interestingly, this means that the variable is even available before it is declared. This feature of JavaScript is informal referred to as hoisting. That is, all variables declared in JavaScript Functions (but not related to assignment) are "advanced" to the top of the function body.
The "advance declaration" operation is performed during "pre-compilation" of the JavaScript engine. Before the code starts to run, please read the related ppt for more details:
Http://www.slideshare.net/lijing00333/javascript-engine (to wall)
Baidu cloud disk link: http://pan.baidu.com/s/1mguZ1OG
For example:
Var scope = 'global ';
Debugger;
Function F (){
Debugger;
Alert (scope); // undefined
Var scope = 'local'; // The variable assigns an initial value here, but the variable itself is defined anywhere in the function.
Alert (scope); // local
Alert (alias); // alias is not defined (undefined)
}
F ();
That is:
Function (){
Alert (scope );
Var scope = 'local ';
Alert (scope );
}
It can be understood:
Function (){
Var scope;
Alert (scope );
Scope = 'local ';
Alert (scope );
}
In addition, I highly recommend the JavaScript authoritative guide, especially a book that many web experts love.