<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? Well, that's what I thought when I sent the test case to her, but after the test output ... The result of the operation is undefined 2 1. At that time, baffled, study + 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!) Interrupts the execution of the script with an undeclared variable or function that throws a fatal error) //The value at this time is underfined for (var i=0;i<3;i++) { alert ("On for Scope:" +i); The value of I is 0, 1, 2, when I jumps out of the loop at 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 remains 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 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 as 1}test2 ();//If at this time (outside the function scope) the output only exists in the function scope of test2 I, J, K-variable will have a 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?") "); Alert not executed (j); Alert not executed (k); Not implemented </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 the global variable A is overwritten, which means that JavaScript performs a full analysis of the entire script file's definition before execution, so the variable A in the//function body is pointed to the internal local variable before the function test () is executed. 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 declares alert (a) in this line; A is still 4, because the previous 4 has been assigned to a. } Test (); alert (a); A is 1, and here is not within 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 defines alert (a) in this line; A is 2, here's A is a local variable Oh! } Test (); alert (a); A is 1, and here is not within function scope, the value of a is the value of the global variable </script>
JavaScript: Talking about the global variables and local variables of JS