The concept of scope in JS:
It indicates the region where variables or functions work. It refers to the context in which they are executed, that is, the context execution environment. There are only two types of Javascript scopes: global and local scopes. Local scopes are differentiated by functions.
First, let's look at several questions:
1.Copy codeThe Code is as follows: if (true ){
Var aa = "bb ";
}
Console. log (aa); // bb
For (var I = 0; I <100; I ++ ){
// Do
}
Console. log (I); // 100
2.Copy codeThe Code is as follows: var bb = '000000 ';
Function aa (){
Alert (bb); // undefine
Var bb = 'test ';
Alert (bb); // test
Var cc = "test1 ";
Alert (age); // syntax error
}
Aa ();
3.Copy codeThe Code is as follows: var test = '20140901 ';
Function aa (){
Alert (test );
}
Function bb (){
Var test = '20140901 ';
Aa ();
}
Bb (); // alert (1111111 );
4.Copy codeThe Code is as follows: alert (typeof aa); // function
Alert (typeof bb); // undefined function aa () {// function Definition
Alert ('I am 111111111 ');
};
Var bb = function () {// function expression
}
Alert (typeof bb); // function
5.Copy codeThe Code is as follows: function aa (){
Var bb = "test ";
Cc = "test ";
Alert (bb );
}
Aa ();
Alert (cc); // Test
Alert (bb); // syntax error
The above five questions all summarize the scope issues in js.
We can summarize several points of view.
I. No block-level scope
It can be seen from the first question that, after execution in {}, the variable is not destroyed or saved in memory, so we can access it.
2. Functions in JavaScript run in their defined scopes, rather than in their executed scopes.
The concept of function scope chain is mentioned here. In ECMA262, this is true.
The scope of any execution context is implemented by the scope chain.
When a function is defined, the scope chain during its definition is linked to the [[scope] attribute of the function object.
When a function object is called, an activity object (that is, an object) is created, and each function parameter is named as the name attribute of the activity object, then, this activity object is used as the frontend of the scope chain at this time, and the [[scope] of this function object is added to the scope chain.
Therefore, the result of Question 3 is alert (1111111 );
Iii. JS will process the function definition and var keywords in advance
For example, in question 4, alert (bb); // undefine, alert (age) // The syntax reports an error. What are the differences between the two statements? The reason is that var bb = "test ", the keyword var is processed in advance during initialization, but this is not assigned a value at the beginning.
You can see that the code is modified like this.Copy codeThe Code is as follows: var dd = '20140901 ';
Function aa (){
Alert (bb); // undefine
Var bb = 'test ';
Alert (bb); // test
Var cc = "test1 ";
Alert (age); // syntax error
}
Aa ();
Alert (dd); // 11111
Alert (cc); // syntax error
Here, alert (bb) does not report a syntax error. alert (age) reports a syntax error.
Note:Copy codeThe Code is as follows: <script>
Alert (typeof aa); // result: undefined
</Script>
<Script>
Function aa (){
Alert ('upeng ');
}
</Script>
This indicates that js pre-compilation is based on segments. Same as question 4
Iv. function-level scope
The variables defined in the function are destroyed after the function is executed and do not occupy the memory area.
Therefore, the alert (cc) at the end of Question 2 has a syntax error. The same applies to alert (bb) at the end of Question 5.