First of all: the scope of JS is different from the scope of other languages, pay special attention.
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, i.e. the context execution environment. There are only two scopes for javascript: global scope and local scope, and local scopes are distinguished by functions.
First look at several topics:
1.
if (true) {
var aa= "BB";
}
Console.log (AA); Bb
for (var i = 0; i <; i++) {
Do
}
Console.log (i); 100
2.
var bb = ' 11111 ';
function aa () {
Alert (BB);//undefine
var bb = ' Test ';
Alert (BB);//test
var cc = "Test1";
alert (age);//Syntax error
}
AA ();
Alert (cc);//Syntax error
3.
var test = ' 1111111 ';
function aa () {
alert (test);
}
function bb () {
var test = ' 22222222 ';
AA ();
}
BB ();//alert (1111111);
4.
Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code
5.
function aa () {
var bb = "Test";
CC = "Test";
Alert (BB);
AA ();
Alert (cc);//test
Alert (BB);//Syntax error
The above 5 topics all summarize the scope of JS in the problem
Can sum up so many points
One, no block-level scope
As you can see from the first question, when executed in {}, the variables are not destroyed or stored in memory, so we can access them.
The functions in JavaScript run in the scope they are defined in, not in the scope in which they are executed.
The concept of the scope chain of a function is mentioned here, in ECMA262.
- The scope of any execution context time is implemented by the scope chain (scope chain).
- When a function is defined, the scope chain that it defines is linked to the [[scope]] property of the function object.
- When a function object is called, an active object (that is, an object) is created, and then, for each function's formal parameter, it is named as the active object's named property, then the active object is the front-end of the scope chain (scope chain) at this time, and this function object's [[ Scope]] added to scope chain.
So the title 3 result is alert (1111111);
JS will process the function definition and the var keyword in advance.
Start alert (BB) as topic 4; Undefine, alert (age)//syntax error, what is the difference between the two, the reason is that there is a var bb = "Test", at the time of initialization to deal with the keyword of Var, but this is not the beginning of the assignment
Modify the code so that you can see
var dd = ' 11111 ';
function aa () {
Alert (BB);//undefine
var bb = ' Test ';
Alert (BB);//test
var cc = "Test1";
alert (age);//Syntax error
}
AA ();
Alert (cc);//Syntax error
Here alert (BB) does not report a syntax error, alert (age) reported a syntax error.
However, please note that:
<script>
Alert (typeof AA); Results: undefined
</script>
<script>
function aa () {
Alert (' Yupeng ');
}
</script>
This means that JS precompilation is a unit of segments. Topic 4 likewise
Iv. Function-level scopes
The defined variables inside the function are destroyed after the function is executed and do not occupy the memory area.
So the topic 2 the last alert (cc); Grammar error, topic 5 finally to alert (BB) the same
On the scopes in JavaScript