js global and local variables

JS declare global variables divided into explicitly declared or implicitly declared
 
Disclaimer way:
Use var (keyword) + variable names (identifiers) in a function declared outside a way, that is, global variables, or in the function declaration is a local variable
Disclaimer way:
Do not use var, directly to the test identifier assignment, it will implicitly declare a global variable test. Even in the statement is a function, the function is executed when the test becomes a global variable.
Statement Three ways:
Use window global object to the statement, the corresponding property of the global object is a global variable
Global variables advantages:
You can reduce the number of variables, reduce the actual parameters and the data transfer parameters in the form of a time to bring consumption.
Disadvantages:
1 take up more memory units
2 as a function of the destruction of the package so that the performance of the function dependence on global variables
3 reduces code readability
Precautions:
 A, Javascript variable scope (scope) is a method of dividing the block (that is to say a function of braces {} divided) . Remember, a function block, and for, while, if the block is not a criteria for the classification scope
 
Two, Javascript will first do a complete analysis (including local variables) to declare part of the entire file before executing the script to determine the scope of real variables .
 
  Third, when the global variable with the same name as a local variable, local variable scope will overwrite the scope of global variables, when leaving the scope of a local variable, and go back to the global variable scope
 
There are a = 10;
function test() {
the console.log (A); / / undefined declared only unassigned
a = 100;
console.log(a); //100
console.log(this.a); //10
There;
console.log(a); //100
 
}
test();

Guess you like

Origin www.cnblogs.com/bjyx/p/11968718.html