- Scope and role definitions
Scope definitions: JavaScript variables stored in variables and find the set of rules.
JavaScript generally follow the lexical scope.
Find Find variables follow from the inside out:
Engine from the current execution scope variables start looking, keep looking up if you can not find one. When arrived at the outermost global scope, either to find or not find, the search process will stop.
- Lexical scoping
In the lexical scope of the definition phase. At the time of writing the code block scope variables and where to write to decide.
- Produced scope
JavaScript has no notion of massive scope. So what is it simply a bulk scope {..} is the block enclosed area is a minimum unit.
We can simply look at the scope and under the stated period or c # variables in the Java language generally is the case
Scope of variables:
Scoped variables defined starting position, the pair of braces that variable where the end;
Life cycle:
Beginning live variable in memory from the location defined above;
Variable arrive in scope when it disappeared in memory;
Let me cite one simple example for loop
C # variable i defined for the outside access cycles not
JavaScript in the outer loop for normal access
Then the scope is created in JavaScript when it
- Global scope
This do not need too much explanation, I believe we all understand
2. Functions
javaScript each create a bubble function will create a scope for itself. All variables belong to this function can be used and reused in the entire internal function
- Massive scope
He says there is no js massive scope, but where there are exceptions, there are several situations js can provide massive scope effect
1: Try/catch
Catch clause will create a massive scope, which declared variables are only valid within the catch
try{
undefined();
}catch(err){
console.log (err); // can access
}
console.log (err); // here to visit the less, ReferenceError: err not found
2: Let
Let variable's scope may be bound {...} where the bulk scope
Var foo=true;
If(foo){
Let bar=foo*2;
Console.log (bar); // normal access
}
Console.log(bar); // ReferenceError
l let conducted using massive scope statement will not be improved.
{
Console.log(bar);// ReferenceError
Let bar=2;
}
3:Const
Const also creates massive scope, but its value is fixed, then any attempt to modify the operation will cause an error.
- Scope upgrade
Function declarations and variable declarations will increase. The assignment or other operational logic will remain in place. When the function declarations and variable declarations occur simultaneously, the function will be lifted first, and then the variable.
Function expression will not be lifted
Lifting scope will be raised to the top of the block where the scope, i.e., where {...} top
<Script type = "text / JavaScript"> foo (); // can not access // var A = to true; { foo (); // get access function foo () { the console.log ( "A"); } } </ Script>
- Scope closure
- Immediately execute the function
- Deceive lexical scope