Javascript learning-about JS global variables, local variables (recommended), and js global variables

Source: Internet
Author: User

Javascript learning-about JS global variables, local variables (recommended), and js global variables

Today, an intern from our company asked me about the differences between the two JS codes:

Code 1:

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

Code 2:

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

I think it's easy. Isn't it about the scope of global variables and local variables? I said: "When the global variable and the local variable have the same name, the scope of the local variable will overwrite the scope of the global variable. After leaving the scope of the local variable, return to the scope of the global variable. Therefore, the result of running the two code segments is: 1) undefined World 2) Hello World. Then I made up the following example for her:

<script>  var a =1;  function test(){    alert(a);    var a = 2;    alert(a);  }  test();  alert(a); </script>

Guess what the result is? Is the output 1 2 1? Well, I thought so before I sent the test case to her, but after the test output ...... The running result is undefined 2 1. At that time, I was puzzled and asked Miss Gu to know that I was not very familiar with JS, so I learned and tested through hard work. The following is a summary:

1. scope of Javascript variables is divided by method blocks (that is, it is divided by a pair of braces {} of Functions ). Remember, it is a function block, and the for, while, And if blocks are not the scope criteria. You can look at the following examples:

<Script> function test2 () {alert ("before for scope:" + I); // I is not assigned a value (not declared! Use undeclared variables or functions to throw a fatal error and Interrupt script execution) // at this time, the I value is underfined for (var I = 0; I <3; I ++) {alert ("in for scope:" + I); // The value of I is 0, 1, and 2. When I is 3, it jumps out of the loop} alert ("after for scope: "+ I); // The value of I is 3. Note that it is already out of the for scope, but the value of I is still 3 while (true) {var j = 1; break;} alert (j); // The value of j is 1. Note that it is not in the while scope, but the value of j is still 1 if (true) {var k = 1;} alert (k); // The value of k is 1. Note that it is already out of if scope, however, the value of k is still 1} test2 (); // If the output does not exist in the functio of test2 at this time (outside the function scope) What happens to the I, j, and k variables in n scope? Alert (I); // error! Yes, it is error. The reason is that variable I is not declared (not the first line output of test2 function is distinguished), resulting in a script error. The program ends here! Alert ("Will this line be printed? "); // Alert (j) not executed; // alert (k) not executed; // not executed </script>

2. Javascript performs a complete analysis (including local variables) on the declaration part of the entire script file before execution to determine the scope of the real variables. How can this problem be solved? Let's look at the following example:

<Script> var a = 1; function test () {alert (a); // a is undefined! This a is not a global variable. This is because a local variable with the same name has been declared in function scope (the last 4th rows of the function body). // Therefore, global variable a is overwritten, this shows that Javascript performs a complete analysis on the definition of the entire script file before execution. Therefore, before the test () function is executed, // variable a in the function body is directed to the internal local variable. instead of pointing to external global variables. however, at this time, a only declares and has not been assigned a value, so undefined is output. A = 4 alert (a); // a is 4. Is there no suspense? Here, a is a local variable! Var a; // The local variable a declares alert (a) in this line; // a is still 4, because 4 has been assigned to a} test (); alert (a); // a is 1, which is not in function scope. The value of a is the value of the global variable. </script>

3. When the global variable is the same as the local variable, the scope of the local variable overwrites the scope of the global variable. After leaving the scope of the local variable, it returns to the scope of the global variable again, when a global variable encounters a local variable, how does one use the global variable? Use window. globalVariableName.

<Script> var a = 1; function test () {alert (window. a); // a is 1. Here, a is a global variable! Var a = 2; // in this line, local variable a defines alert (a); // a is 2. Here, a is a local variable! } Test (); alert (a); // a is 1, which is not in function scope. The value of a is the value of a global variable. </script>

Of course, for more articles, refer to the following articles.

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.