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:
Copy codeThe Code is as follows:
Function t (){
Var;
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.
Copy codeThe Code is as follows:
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, and there is no relevant var statement in the global object.
// Because there is no relevant var statement, js implicitly declares the variable var all in the function;
All = 30;
Alert (all );
}
Test1 ();
Alert (all );
Alert (window. all );
2. variables defined in the function (except for functions in the function) are valid within the function.
Copy codeThe Code is as follows:
Function test2 (){
Var t = 0;
// Define variables in the for condition. The change's scope chain object is the function.
// It is valid in the entire function.
For (var I = 0; I <5; I ++ ){
T + = I;
}
Alert (I );
}
Test2 ();
3. Replace the global variable with the same name in the function.
Copy codeThe Code is as follows:
Var t = "bb ";
Function test (){
// When t is executed, it first looks for the scope chain object. Because it is defined inside the function, this function is the first object of its scope chain.
// T is defined in this object, so t is a local variable, which replaces the global variable t.
// T is defined at this time, but no value is assigned. The value is assigned to the next row, so undefined is output here.
Alert (t );
Var t = "aa ";
Alert (t );
}
Test ();
4. No block Scope
Copy codeThe Code is as follows:
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 is output.
Alert (tmp );
The following content comes from the summary of reading online blogs. When using the notes, I only want to focus on them, and I am very grateful to the bloggers who are willing to share them. You have made me stand on the shoulders of giants!
1,
Copy codeThe Code is as follows:
Var temp = (function (){
Var name = "test ";
Return function (){
Alert (name );
}
})();
The above code snippet is a style that we often see in jser. It is the legendary closure. As we all know, calling temp (); "test" will pop up. This process can be explained based on the following three theories:
1) js scopes are only related to function delimiters. nested functions form a scope chain;
2) The create rule of the scope chain is to copy the scope chain of the previous stage, and put the pointer pointing to the environment variable object at the beginning of the chain;
3) In Javascript, if an object is no longer referenced, the object will be recycled by GC. If two objects are referenced by each other and no longer referenced by 3rd, the two objects referenced by each other will be recycled.
If you do not understand the above three items, you can refer to the detailed explanation of the code based on the theory below:
The outer function is destroyed after execution. However, the outer function's scope chain is copied to the scope chain of the inner function, which is part of the scope chain of the inner function. Remember that it is replication, not a reference (based on 2nd entries), so the inner function can still access the name. Because the inner function returned is referenced by temp, after the outer function is destroyed, although the inner function is part of the outer function, it still exists. As with article 3rd, It is referenced by a third party. The so-called closure is also the principle.