Js scope chain and variable scope

Source: Internet
Author: User
Tags variable scope

To understand the scope of a variable, you must first understand the scope chain.
When a variable is declared with the var keyword, an attribute is added to the object where the variable is located.
Scope chain: Because js variables are all attributes of objects, this object may be attributes of other objects, and all objects are properties of window objects, therefore, the relationship between these objects can be considered as a chain.
The chain header is the object where the variable is located, and the chain tail is the window object.

See the following code:

function t() { var a; function t2() { var b; } }

 

In js, functions are also objects, so the object where variable a is located is t, and variable t is in the window object, so the scope chain of variable a is as follows:
T -- window
So the objects that B is in, that is, t2 and t2, are included in t, and t is in the window object. Therefore, the scope chain of B is as follows:
T2 -- t -- window
After understanding the scope chain, we started to analyze the scope of the variable.

1. All variables without var in javascript are global variables and are properties of the window object.

Function test1 () {// when executing this sentence, it will find the scope object. This function is the first object in the scope chain, but there is no relevant var statement in this object // find the second object of the scope chain, that is, the global object, there are no relevant var statements in the global object. // because there are no relevant var statements, js implicitly declares the variable var all; all = 30; alert (all);} in the function );} test1 (); alert (all); alert (window. all );

 

2. variables defined in the function (except for functions in the function) are valid within the function.

Function test2 () {var t = 0; // define the variable in the for condition, the scope chain object of this change is this function // Therefore, it is valid for (var I = 0; I <5; I ++) in the entire function) {t + = I;} alert (I);} test2 ();

 

3. Replace the global variable with the same name in the function.

Var t = "bb"; function test () {// when t is executed, it first looks for the scope chain object, because it is defined inside the function, so this function is the first object of its scope chain // and t is defined in this object, so t is a local variable, it replaces the global variable t // t, which is defined at this time, but is not assigned a value. The value is assigned to the next row, so undefined alert (t); var t = "aa" is output here "; alert (t);} test ();

 

4. No block Scope

 

If (true) {// defines a variable in the block. The first object of its scope chain is the Global Object window var tmp = 0 ;} // The first object of the tmp scope chain is the Global Object window, and there is a var statement related to the global object above, so 0 alert (tmp) is output );

 

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.