Deep understanding of JavaScript variable scopes

Source: Internet
Author: User
Tags variable scope

  JS Variable Scope Features:

The scope of a and JS variables is based on its unique scope chain.

b, JavaScript does not have a block-level scope.

c, the variables declared in the function are defined throughout the function.

1. Scope Chain

 var  rain = 1   var  man = 2  ; function inner () { var  innervar = 4  ;    alert (rain);     } inner ();  //  call inner function       }rainman ();  //  call Rainman function   

Observe the alert (rain); this code. JavaScript first looks in the inner function to see if the variable rain is defined, and if defined, uses the rain variable in the inner function, and if the rain variable is not defined in the inner function, JavaScript continues to look for a rain variable in the Rainman function, where the rain variable is not defined in the Rainman function body, and the JavaScript engine continues to look up (the global object) to see if rain is defined. In the global object we define rain = 1, so the final result will pop up ' 1 '.

Scope chain: When JavaScript needs to query a variable x, it first looks for the first object in the scope chain, and if the x variable is not defined with the first object, JavaScript continues to find out if the x variable is defined, and if the second object does not have a definition, it will continue to find, and so on.

The above code involves three scope chain objects, followed by: Inner, Rainman, window.

2, inside the function body, the local variable priority is higher than the global variable with the same name.

var 1 ;     //  function Check () {    var ;  //     alert (rain);     //  }check (); alert (rain);      //

3. JavaScript does not have block-level scopes.

This is also part of JavaScript that is more flexible than other languages.

Looking closely at the code below, you will find that the variables I, j, K scopes are the same, they are global in the entire rain function body .

function Rainman () {//There are three local variables in the body of the Rainman function I j k    vari =0 ; if(1 ){        varj =0 ;  for(varK =0; K <3; k++) {alert (k);//pop up 0 1 2 respectively} alert (k); //in the For loop, it still pops out 3.} alert (j); //outside of the if statement, but still pops up 0}

4. Variables declared in a function are defined throughout the function.

  Declaring a pre-rule: The Declaration is advanced, but the assignment is still assigned on the line of the assignment; before this is undefined

function  Rain () {    var  x  =   1  ;    Function Man  () {        x  = +     ;    }    Man ();      //     alert (x);     //  }rain ();      //

The code above shows that the variable x can be used throughout the rain function, and can be re-assigned (of course, the function does not call the man () method, X or Pop 1). As a result of this rule, it will produce "unthinkable" results, observe the following code.

var  X  =   1  ; function  Rain () {    alert (x);      //     var  x  =   '  ;    alert (x);      //  }rain ()

This is because within the function rain the local variable x is defined in the entire function body (var x= ' Rain-man '), so the global variable x with the same name is hidden within the entire rain function body. The reason why ' undefined ' pops up here is that the local variable x is still not initialized when the first alert (x) is executed.

So the above code is equivalent to:

function  Rain () {    var  x;    alert (x);    X  =    " ;    alert (x);}

5. Variables that are not defined with the VAR keyword are global variables.

function  Rain () {    x  = +    ;     //  }rain (); alert (x);      //

This is also a common mistake for JavaScript novices, leaving many global variables unintentionally.

6, global variables are properties of the Window object

var  X    =+  // popup True

Deep understanding of JavaScript variable scopes

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.