First, scope
Almost all languages have the concept of scope, simply put, scopes are the accessible scope of variables and functions, that is, scope controls the visibility and life cycle of variables and functions.
In JavaScript, the engine, compiler, and scope co-ordinate the execution of JavaScript.
Engine: responsible for compiling and executing JavaScript code from beginning to end
Compiler: responsible for lexical analysis and code generation
Scope : responsible for collecting and maintaining a series of queries consisting of declared variables and executing a very strict set of rules to determine the access rights of the currently executing code to these variables
Ii. examples
2.1 Scopes scoped by code block
public void Func () { if (1==1) { string name = ' java '; } Console.WriteLine (name); } Func ()
This code is an error, in a code block scoped language, the scope of {} is the scope of a variable. The scope range of the function name variable in the IF Condition statement, Console.WriteLine is not the variable that gets the name, can be changed to:
public void Func () { if (1==1) { string name = ' java '; Console.WriteLine (name); } Console.WriteLine (name);} Func ()
2.2 Scopes scoped by function
#pythondef func (): if 1==1: name = ' python ' print (name) func ()
This code works correctly, although the declaration of the name variable is within the indent of the IF, but the Python language is scoped to the function. That is, within the function, a variable has been declared, then it can be called by the following code.
Third, the scope of JavaScript
The scope of 3.1 JavaScript is also scoped to the entire function.
# with the entire function scoped to the scope function func () { if (1==1) { var name = ' Javascript '; } Console.log (name);} Func ()
Console run:
3.2 Scopes have been created before they are called
In the above code, the compiler (browser) interprets execution from top to bottom, and the scope is clear, not until the Func () function is called.
3.3 Scope nesting
The engine looks for the variable from the current execution scope, and if it does not, continues the lookup up to the outermost global scope chain, regardless of whether the variable is eventually found, to the end of the lookup process.
name = ' Bigberg '; function func () { var name = ' Eric '; function inner () { var name = ' Sam '; Console.log (name); } return inner;} res = func (); Res ();
The closest principle, the output is Sam. If you comment out the Sam Line, what's the result?
name = ' Bigberg '; function func () { var name = ' Eric '; function inner () { //var name = ' Sam '; Console.log (name); } return inner;} res = func (); Res ();
The res () function appears to be executed outside the scope of the function, and the output should be Bigberg. However, according to 3.2, the scope of the function is determined before the function is called, then the nearest principle is that the output should be Eric.
So if you make the following changes:
name = ' Bigberg '; function func () { var name = ' Eric '; function inner () { //var name = ' Sam '; Console.log (name); } var name = ' Tom '; return inner;} res = func (); Res ();
What is the output of this situation? According to the nearest principle to find, the inner function does not have the name of the variable, then go to the previous layer to find. In the Func function, when the compiler interprets execution, the first time Eric assigns the variable to name, and then assigns the value of Tom to the name variable, Eric is covered by Tom, so the output of res () should be: Tom
3.4 JavaScript Internal local variables are declared in advance
function func () { console.log (name); var name = ' Bigberg '; }
In JavaScript, local variables are found in advance to do such an operation, such as:
var name = ' Bigberg '; var name;name = ' Bigberg ';
If the variable is not assigned a value, it is assigned the default value of undefined.
So in the above code, the value of the Func () function output should be undefined. Although name = ' Bigberg ', when Console.log (name) executes, the variable name only executes the Var name operation, which is assigned the default value of undefined.
Scope of JavaScript