The scope of JavaScript has always been a more difficult point to understand in front-end development, and a few words for the scope of JavaScript
One, no block-level scope in JavaScript
There is a block-level scope in Java or C #, which is: curly braces are also a scope.
void Main () { if(1==1) { = "Seven"; } SYSTEM.OUT.PRINTLN (name);} // Error Java
in Java
void Main () { if(1==1) { = "Seven"; } Console.WriteLine (name);} // Error C #
in C #
No block-level scopes in the JavaScript language
function Main () { if(1==1) { var name = ' seven '; } Console.log (name);} // output: Seven
Second, use function scope in JavaScript
In JavaScript each function acts as a scope, and the variables in the internal scope cannot be accessed externally.
function Main () { var innervalue = ' Seven '// Error: uncaught referenceerror: Innervalue is not defined
Third, the scope chain in JavaScript
Because each function in JavaScript acts as a scope, the scope chain appears if function nesting functions occur.
XO = ' Alex '; function Func () { var xo = "Seven"; function inner () { var xo = ' Alvin '; Console.log (XO); } Inner ();} Func ();
As shown above, there are three scopes of scope chain, if the scope chain, then the search for variables will appear in order, for the above example:
When executing Console.log (XO), its search order is based on the scope of the chain from the inside to the outside of the priority, if the inner layer does not step up to find, until the throw exception is not found.
Iv. the scope chain in JavaScript was created before it was executed
The scope of JavaScript is created before it is executed, and it is only necessary to follow the scope chain to find out when to execute it later.
Example One:
XO = ' Alex 'function Func () { var xo = "Seven"; function inner () { console.log (XO); } return var ret = Func (); ret (); // output Result: seven
The above code, before the function is called, the scope chain already exists:
- Global scope, Func function scope, inner function scope
When executing "ret ();", because its surrogate refers to the inner function, the scope chain of this function has been defined before execution: global scope, Func function scope, and inner function scope, so, when executing "ret ();", The variable is searched based on the existing chain of scopes.
Example two:
XO = ' Alex 'function Func () { var xo = "Eirc"; function inner () { console.log (XO); } = ' seven '; return var ret = Func (); ret (); // output Result: seven
The code above is the same as example one, and it also emphasizes that the scope chain already exists before the function is called:
- Global scope, Func function scope, inner function scope
At different time, the value of the XO variable in the FUNC scope has been reset from "Eric" to "seven" when executing "var ret = Func ();", so you can only find "seven" when you Execute "ret ()".
Example three:
XO = ' Alex ';<br>function Bar () { function Func () { var xo = "Seven"; return var ret = Func (); ret (); // output Result: Alex
The code above has created two scope chains before the function is executed:
- global scope, bar function scope
- Global scope, Func function scope
When performing "ret ();", the RET surrogate refers to the bar function, and the bar function's scope chain already exists: global scope--bar function scope, so execution will be based on the existing scope chain to find.
V. Declaration of Advance
In JavaScript, if you do not create a variable and go directly to it, the error is:
Console.log (xxoo); // error: uncaught referenceerror:xxoo is not defined
If you create a value without assigning a value in JavaScript, the value is undefined, such as:
var Xxoo;console.log (xxoo); // Output: Undefined
Within the function, if you write this:
function Foo () { console.log (XO); var xo = ' seven ';} Foo (); // Output: Undefined
The above code, not error, but output undefined, the reason is: the JavaScript function before it is executed, the variables are all declared, without assigning value. Therefore, the equivalent of the above example, the function in "precompiled" when the Var XO has been executed, so the above code output is undefined.
HTML, CSS, javascript-7 JavaScript scopes