Javascript variable scope

Source: Internet
Author: User

As we all know, there are only two types of variable scopes in javascript, global scopes and local scopes in functions (Some people think that there is also a scope between different script nodes, called segment scopes, the reason is the variable defined in the subsequent script node. Before this script node is parsed, the previous script node cannot access this variable, this special case depends on the code parsing order, which is not discussed in this article ).

For example, the following code:

var a = 1;
function f(b){
var c = 2;
}

A is the global variable we declare, c is the local variable we declare, and B is the form parameter of function f and also a local variable.

Let's look at the following code:

function outer(){
var o;
function inner(){
var i;
}
}

We can see that o and I are both local variables, except that the scope of o is the function body of the function outer, and the scope of I is the function body of the inner.

Let's look at another piece of code:

var g = 1; function outer(){     var o = 1;     function inner(){         var i = 1;         debugger;     }     inner(); } outer();

Debugger? That is, debugger. We run this Code separately to open the debugging environment of the browser, such as the firebug of the ff browser.

Where does a function such as lift () come from?

Let's look at the following code:

debugger;

As you can see, only one scope1.html () function is called when debuggeris run independently.

We know that no custom function is called at this time, so where does this function come from? We may wish to make the following bold guesses: This is automatically generated by the browser's js engine. All our code runs in a browser's predefined function, the variables declared in this function are what we call global variables.

In this way, we can look at javascript code in a consistent way: All code runs in function mode, and javascript has only one variable scope, that is the local scope of the function.

The above comments are purely personal opinions. If you have different opinions, you are welcome to make a picture.

Ps:

1. The name of the scrope1.html () function may be different in different debugging environments. For example, in ie, the global script code is used, and in chrome, the anonymous function is used.

2. We recommend that you run the above Code to check the call stack and variable monitoring status when the function is running, it is helpful for understanding the concepts of javascript such as scope, scope chain, and closure.

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.