On the _javascript techniques of global variables and local variables in JavaScript

Source: Internet
Author: User

One, JavaScript scope is divided into function blocks, not as if, while, for.

<script>
Function F1 () {
   alert ("before for Scope:" +i);    
 I was not assigned a value (not without a declaration!) Using undeclared variables or functions can cause fatal errors to break script execution)
 //At this point I value is undefined for
   (var i=0; i<3;i++) {
       alert (' In for scope: ' +i);}
 The value of I is 0,1,2  
   alert ("After for scope:" + 1);
  The value of I is 3, which is already outside of for scope, but the value of I remains at 3 while
    (true) {
       var j=1;
       break;
    Alert (j);
  The value of J is 1, which is already outside of the while scope, but the value of J is still retained as 1
    if (true) {
      var k=1;
    }
    Alert (k);
  The value of K is 1, which is already outside the if scope, but the value of K remains at 1
}
F1 ();
In this case, the function is transferred out of the function block, and the I j K Variable
alert (i), which exists in F1 this function scope, is again exported;
Error!!! The reason is that I did not declare here (not unassigned, F1 the first line of output), script error, end of program!
alert (j);
alert (k) not executed;
//Not executed
</script>

Second, JavaScript compiles the scope of a real variable by precompiling the entire script file (analyzing the declarative part of the script file, including the local variables section) before executing. For example, below:

<script>
   var x=1;
   function F2 () {
    alert (x);
   The value of X is undefined! This x is not a global variable because the function scope has declared a local variable with the same name, so the parameter a of the global variable is overwritten.
    shows that JavaScript is precompiled before execution, and X in the function body is directed to local variables rather than global variables. At this time x only declares, has not assigned the value, therefore is undefined
    x=3;
    alert (x);
   The x value is 3, but it is also a local variable
    , var x;
   Local variable x here declares
    alert (x);
   Value is 3
   }
   f2 ();
   alert (x);
   The x value is 1, not within the function scope, and the value of x is the value of the global variable.
</script>

When the global variable has 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 a local variable,

How do you use global variables? With Window.globalvariablename.

<script>
   var a=1;
    Function F3 () {
       alert (WINDOW.A);
  A bit 1, here's A is the global variable
       var a=2;
        alert (a);
      }
    F3 ();
    alert (a);
</script>

The above part of the JavaScript of the global variables and local variables are small parts to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.