var a=5;function fun () {a=0; alert (a); 0 alert (THIS.A); 5 var A; alert (a); 0}fun ();
The above code will output 0, 5,0.
first of all, our variable declaration assignment in JS is divided into two parts.
1. Variable declaration var a;2. Variable assignment a=2;
But in general we will put the declaration and assignment of variables together is Var a=2;
and the declaration of the variable in JS will be advanced to the beginning of the current scope, that is to say the above code and the following code is equivalent
var a=5;function fun () { var a; Declaration of //variables in advance a=0; //assignment statement remains constant alert (a); //0 alert (THIS.A); //5 alert (a); //0 }
so alert (a) is actually the value of the output local variable A, THIS.A is the value of the global variable A, the above is the declaration of the variable is introduced in advance
below our code will be modified as follows
var a=5;function fun () {a=0; alert (a); 0 alert (THIS.A); 0 alert (a); 0}
at this time all of our output values are 0;
We declare variables to be divided into a display declaration (Var a) and an implicit declaration (a);
when a local variable inside a function is implicitly declared, the variable is not a local variable, but a global variable.
so it is equivalent to assigning a value of 0 to the global variable A, so the output is all a.
This article is from the "11722655" blog, please be sure to keep this source http://11732655.blog.51cto.com/11722655/1789878
JS (JavaScript) Small talk variable declaration (explicit implicit declaration, declaration in advance)