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>
I think it's easy, isn't it the scope of global variables and local variables? I said, "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.
Here's an example:
- <SCRIPT>&NBSP;&NBSP;
- VAR&NBSP;A&NBSP;=1;&NBSP;&NBSP;
- 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, think of the solution, asked the Valley teacher know, I am not very understanding of 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 time 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 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 is still reserved for 3
- While (true) {
- var j = 1;
- Break ;
- }
- Alert (j); the value of//J is 1, note that at this point, the value of J remains at 1 except for the while scope.
- if (true) {
- var k = 1;
- }
- Alert (k); the value of//k is 1, note that at this point it is outside the if scope, but the value of K still remains 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, distinguishes 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 implemented
- Alert (j); //Not implemented
- Alert (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 for undefined! This A 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 () is executed,
- The variable A in the body of the function 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, 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 is still 4, because I've already assigned 4 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 is defined in this row
- alert (a); //a is 2, here's A is the global 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: Talk about JS's global variables and local variables "go"