JavaScript: Talking about the global variables and local variables of JS

Source: Internet
Author: User

The difference between the two-segment JS code:

<script type= "Text/javascript" >   var a = "Hello";    function Test () {        var  A;        alert (a);         = "World";        alert (a);   }  

<script type= "Text/javascript" >   var a = "Hello";    function Test () {        alert (a);         = "World";        alert (a);   }   </script>  

"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 look at one of the following examples:

<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. When so painstaking effort, 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>functiontest2 () {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(vari=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){          varj = 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){          varK = 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, 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 executedAlert (j);//not executedAlert (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>varA =1; functionTest () {alert (a);//A is 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 () executes,                     //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=4alert (a); //A is 4, no suspense, right?  Here's a or a local variable Oh!         varA//local variable A is declared in this linealert (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 ();       // A is 1, and here is not within the 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.