The concept of scope in JS:
Represents the area in which a variable or function works, referring to the context in which they are executed, that is, the context execution environment. JavaScript has only two scopes: global scope and local scope, and local scope is differentiated by function.
First look at a few questions:
1.
Copy Code code as follows:
if (true) {
var aa= "BB";
}
Console.log (AA); Bb
for (var i = 0; i < i++) {
Todo
}
Console.log (i); 100
2.
Copy Code code as follows:
var bb = ' 11111 ';
function aa () {
Alert (BB);//undefine
var bb = ' Test ';
Alert (BB);//test
var cc = "Test1";
alert (age);//Syntax error
}
AA ();
3.
Copy Code code as follows:
var test = ' 1111111 ';
function aa () {
alert (test);
}
function bb () {
var test = ' 22222222 ';
AA ();
}
BB ();//alert (1111111);
4.
Copy Code code as follows:
Alert (typeof AA); function
alert (typeof BB); Undefined function aa () {//functions definition
Alert (' I am 111111111 ');
};
var bb = function () {//functions expression
}
alert (typeof BB);//function
5.
Copy Code code as follows:
function aa () {
var bb = "Test";
CC = "Test";
Alert (BB);
}
AA ();
Alert (cc);//test
Alert (BB);//Syntax error
The above 5 topics all summed up the scope of JS problem
Can sum up a few points
First, no block-level scope
As you can see from the first question, after execution in {}, the variable is not destroyed or stored in memory, so we can access it.
Second, the functions in JavaScript run in their defined scopes, not in the scopes in which they are executed.
This refers to the concept of the scope chain of functions, which is ECMA262 in the
The scope of any execution context moment is implemented by the scope chain (scope chain).
When a function is defined, the scope chain that it defines will be linked to the [[scope]] property of the function object.
When a function object is called, the creates an active object (that is, an object), and then, for each function's formal parameters, is named the active object's named property, and then the active object is the front end of the scope chain (scope chain) of the function object, and the Scope]] is added to the scope chain.
Therefore the topic 3 turned out to be alert (1111111);
Third, JS will deal with function definitions and var keyword ahead of time
As topic 4 start alert (BB); Undefine, alert (age)//grammar error, what is the difference between the two, the reason is that there is a var bb = "Test", in the initialization of the time to deal with the keyword of Var, but this is not the beginning of the assignment
To change the code into this, you can see
Copy Code code as follows:
var dd = ' 11111 ';
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
Alert (BB) does not report syntax errors here, alert (age) reported syntax errors.
But please note:
Copy Code code as follows:
<script>
Alert (typeof AA); Result: undefined
</script>
<script>
function aa () {
Alert (' Yupeng ');
}
</script>
This shows that JS Precompilation is a segment-unit. Topic 4 Empathy
Iv. Function-level scopes
The variables defined within the function are destroyed after the function has been executed and do not occupy the memory area.
Therefore topic 2 The last alert (cc); syntax error, topic 5 finally to alert (BB) empathy