Javascript: Talk about JS's global variables and local variables "go"

Source: Internet
Author: User

The difference between the two-segment JS code:

 
    1. <script type="Text/javascript" >
    2. var a = "Hello";
    3. function Test () {
    4. var A;
    5. alert (a);
    6. A = "world";
    7. alert (a);
    8. }
    9. </script>

    1. <script type="Text/javascript" >
    2. var a = "Hello";
    3. function Test () {
    4. alert (a);
    5. A = "world";
    6. alert (a);
    7. }
    8. </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:

 
    1. <SCRIPT>&NBSP;&NBSP;
    2.    VAR&NBSP;A&NBSP;=1;&NBSP;&NBSP;
    3.     Function test () {  
    4.       alert (a);  
    5.       var a = 2;   
    6.       alert (a);   
    7.     }  
    8.    test ();   
    9.     alert (a);   
    10. </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:

  1. <script>
  2. function Test2 () {
  3. 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)
  4. //The value of I at this time is underfined
  5. For (var i=0;i<3;i++) {
  6. Alert ("in for Scope:" +i); the value of//I is 0, 1, 2, when I jumps out of the loop at 3 o'clock
  7. }
  8. 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
  9. While (true) {
  10. var j = 1;
  11. Break ;
  12. }
  13. 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.
  14. if (true) {
  15. var k = 1;
  16. }
  17. 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
  18. }
  19. Test2 ();
  20. 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?
  21. 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!
  22. Alert ("Will this line print also output?") "); //Not implemented
  23. Alert (j); //Not implemented
  24. Alert (k); //Not implemented
  25. </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:

 
  1. <script>
  2. var a = 1;
  3. function Test () {
  4. 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) .
  5. //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,
  6. 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.
  7. A=4
  8. alert (a); //a is 4, no suspense, right?  Here's a or a local variable Oh!
  9. var A; //local variable A is declared in this line
  10. alert (a); //a is still 4, because I've already assigned 4 to a.
  11. }
  12. Test ();
  13. alert (a); //a is 1, and here is not within function scope, the value of a is the value of the global variable
  14. </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.

 
    1. <script>
    2. var a = 1;
    3. function Test () {
    4. alert (WINDOW.A);  //a is 1, here's A is the global variable Oh!
    5. var a=2; //local variable A is defined in this row
    6. alert (a);  //a is 2, here's A is the global variable Oh!
    7. }
    8. Test ();
    9. alert (a); //a is 1, and here is not within function scope, the value of a is the value of the global variable
    10. </script>

Javascript: Talk about JS's global variables and local variables "go"

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.