The difference between the two-segment JS code:
<script type= "Text/javascript" >
var a = "Hello";
function Test () {
var A;
alert (a);
A = "world";
alert (a);
}
</script>
<script type= "Text/javascript" >
var a = "Hello";
function Test () {
alert (a);
A = "world";
alert (a);
}
</script>
is not the scope of global variables and local variables? When a global variable has the same name as a local variable, the scope of the local variable overrides the scope of the global variable, and when it leaves the scope of the local variable, it returns to the scope of the global variable. So two pieces of code run the results are: 1) undefined World 2) Hello world. Then I randomly made the following example to her:
<script>
var a = 1;
function Test () {
alert (a);
var a = 2;
alert (a);
}
Test ();
alert (a);
</script>
What do you think the result equals? is the output 1 2 1? MM-Hmm, but after the test output ... The result of the operation is undefined 2 1. At that time, I can not think of its solution, then know, I am not very familiar with JS, so painstaking effort, learning + test, summarized as follows:
The scope of a JavaScript variable is divided by the method block (that is, by a pair of curly braces {} of function). Remember, it is a function block, and for, while, if block is not the scope of the classification criteria , you can look at the following several examples:
<script>
function Test2 () {
Alert ("Before for Scope:" +i); I is not assigned (not declared!) Interrupt script execution with an undeclared variable or function that throws a fatal error completely)
The value of I at this point is underfined
for (Var i=0;i<3;i++) {
Alert ("In for Scope:" +i); The value of I is 0, 1, 2, when I jumps out of the loop 3 o'clock
}
Alert ("After for scope:" +i); The value of I is 3, note that this is already outside of the for scope, but the value of I still remains at 3
while (true) {
var j = 1;
Break
}
Alert (j); The value of J is 1, note that it is now outside the while scope, but the value of J still remains at 1
if (true) {
var k = 1;
}
Alert (k); The value of K is 1, note that it is now outside the if scope, but the value of K is still reserved at 1
}
Test2 ();
If at this time (outside the function scope) the output only exists in the function scope of the test2, I, J, K variables will be the divine horse effect?
alert (i); error! Yes, it is error, because the variable i is not declared (not unassigned, distinguish the first line of the Test2 function output), resulting in a script error, the program to the end!
Alert ("Will this line print also output?") "); Not executed
Alert (j); Not executed
Alert (k); Not executed
</script>
JavaScript performs a full analysis of the declaration portion of the entire script file (including local variables) before execution to determine the scope of the real variable . How do you understand it? Take a look at the following example:
<script>
var a = 1;
function Test () {
alert (a); A is undefined! This is not a global variable, because a local variable with the same name is already declared in function scope (the 4th line of the functional body).
So global variable A is overwritten, which means that JavaScript performs a full analysis of the entire definition of the script file before execution, so before the function test () executes,
The variable A in the function body is pointed to the inner local variable. Instead of pointing to the external global variable. But at this point a only declaration, has not been assigned value, so output undefined.
A=4
alert (a); A is 4, no suspense, right? Here's a or a local variable Oh!
var A; Local variable A is declared in this line
alert (a); A or 4, because I've already assigned a 4 to a.
}
Test ();
alert (a); A is 1, and here is not within the function scope, the value of a is the value of the global variable
</script>
Thirdly, when the global variable is the same name as the local variable, the scope of the local variable overrides the scope of the global variable, and when it leaves the scope of the local variable, it returns to the scope of the global variable, and when the global variable encounters the local variable, how to use the global variable? with Window.globalvariablename.
<script>
var a = 1;
function Test () {
alert (WINDOW.A); A is 1, here's A is the global variable Oh!
var a=2; Local variable A is defined in this row
alert (a); A is 2, here's A is a local variable Oh!
}
Test ();
alert (a); A is 1, and here is not within the function scope, the value of a is the value of the global variable
</script>
JS global variables and local variables