The following is for ES5
First, the variable promotion:
1, Global execution Context (script): variable definition, function declaration (before execution)
// undefined var a = 20;
The above code is equivalent to:
var // The variable is promoted to the top of the global execution context, and the default value is undefined // undefineda = 20;
Look at the function declaration again:
// ZS function FN (name) { =; Console.log (name, age); var Age ;}
The above code is equivalent to:
function // function declaration overall promoted to the top of the global execution context var age; // the variable is promoted to the top of the function execution context age =; Console.log (name, age);} FN (//ZS
Note: You still want to standardize the writing function. Do not recommend the above-mentioned wording, to first define after execution, increase readability. In addition, notice the difference between function declaration and function expression, function declaration can be executed before the definition, function expression can not, will be an error. Uncaught typeerror:fn1 is not a function
2, function execution context: variable definition, function declaration, this,arguments (before execution)
Second, JS scope: 1, no block-level scope
// no block-level scopes if (true) { var name = ' Lily '//Lily
The above code is equivalent to the following code (recommended as follows):
var name; if (true) { = ' Lily '//Lily
2, only function and global scope scope chain: When attempting to access a variable, the JS engine starts looking up from the current scope until the global scope stops, and the ordered collection of scopes becomes the scope chain. Three, this is a few different uses of the scene note: this to be executed in order to confirm the value, the definition can not confirm 1, as a constructor execution 2, as a normal function execution 3, as an object property execution 4,call,apply,bind Four, the closure is not finished to be continued ...
JavaScript scopes and closures