JS Scope and scope chain

Source: Internet
Author: User
Tags variable scope

JS Scope and scope chain

http://blog.csdn.net/yueguanghaidao/article/details/9568071

One: function scope

The first sentence output is: "Undefined", not "global"

The second output is: "Local"

You might think that the first sentence will output: "Global" because the code has not executed var scope= "local", so it will definitely output "global".

I said the idea was absolutely right, just using the wrong object. We will first distinguish between the function scope of JavaScript and the block-level scope of our well-known C + +.

In C + +, each piece of code inside the curly braces has its own scope, and the variables are not visible outside the code snippet that declares them. JavaScript does not have a block-level scope at all, but a function scope.

The so-called function scope is that:-variables are defined in the body of the function that declares them, and in any function within which the body of the function is nested.

So according to the meaning of the function scope, the above code can be rewritten as follows:

As we can see, because of the function scope, local variables are always defined by the whole function body, we can declare the variable "advance" to the top of the function body, and the variable initialization is still in the original position.

Why JS does not have block-level scope, there is the following code for proof:

All output is "local", if there is a block-level scope, the obvious if statement will create a local variable name, and will not modify the global name, but there is no such, so JS does not have block-level scope.

Now it's good to understand why you can get that result. The scope declaration overrides the global scope but has not yet been assigned, so the output: "Undefined".

So the following code is very well understood.

Two: Variable scope

Variables that are not declared with Var in JS are global variables

Three: Scope chain

When you execute S, the execution environment (the calling object) of the function S is created, the object is placed at the beginning of the list, and then the calling object of the function T is linked after, and finally, the global object. Then looking for the variable name from the beginning of the list, it's obvious

Name is "Slwy".

But when the SS () is executed, the scope chain is: SS ()->t ()->window, so name is "Tlwy"

When the document is loaded and a few buttons register the click event, what will pop up when we click the button?

It's easy to make a mistake, yes, three buttons all pop up: "Button4", are you correct?

When the registration event ends, the value of I is 4, and when the button is clicked, the event function is functions () {Alert ("button" +i);} This anonymous function does not have I, according to the scope chain, so to find in the Buttoninit function, when I is the value of 4,

So pop up "Button4".

Attached: saving variables with closures

Four: With statement

When it comes to scope chains, you have to say with statements. The WITH statement is primarily used to temporarily extend the scope chain, adding objects in the statement to the scope's head.

    1. JS does not have block-level scope (you can do it yourself or other methods), only the function-level scope, the function outside the variable function can be found inside the function of variables outside the variable cannot be found.
    2. The search for variables is the nearest principle, looking for variables defined by VAR, to look for the outer layer when it is not found nearby.
    3. The precedence is equivalent when the parameter has the same name as the local variable. When a parameter is passed, the base type is passed, and the reference type is referenced.

1, JavaScript does not have the concept of code block scope, local scope is for the function.

2. If a variable declared with VAR is not used, the default is the global variable

JS Scope and scope chain

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.