Variable scope
Have the scope and life cycle.
Static scope:
Lexical function, is determined by the definition of the location of the program, at the time of compilation determines the life cycle and the scope of action.
is independent of the Code execution order and is parsed by code nesting.
Example:
var x = ten; function Fun () { // compile, X is initialized. alert (x); } function foo () { var x =; Fun (); // when the fun is running, X has been compiled. } foo (); // Ten
Dynamic action:
is determined by the program runtime. A dynamic binding similar to C + +.
is related to the order in which the code is executed.
Through the stack to solve, from top to bottom to find the nearest one.
Variable scope of JS
JS using static scopes
JS does not have the concept of block-level scope, but the appearance of ES6, resulting in another, let. In fact, ES6, nothing advanced, but added some grammatical rules,
Copy this language, that language only.
There is a concept of lexical environment in ES5. Used to manage static scopes
Lexical environment. An object that describes the environment.
Environment record: Record the environment inside
-Formal parameters
-function declaration
-Variable
References to external environments (outer)
Initialization of the environment record: that is, the declaration is in advance. A function declaration similar to the C language.
Before executing, scan the function contents, first define to the environment record inside.
var x = 10< span> function foo (y) { var z = 30; function fun (x) {alert (x + y + Z); return fun; // before performing an environment initialization. The Foo function has a outer variable pointing to the outer environment. var bar = foo (20 // 60 environment record initialization: y =
fun-->> <function> scope within funcion-->> point to Foo environment Z = undefined outer -->> point to Golbal enverioment
Variables are found through the chain of action to the nearest variable value. To parse.
Solved by outer point.
Special Case-with
A temporary lexical environment is created.
The with outer points to global globally. var a = ' aaa ';
<script type= "Text/javascript" >var a = ' AAA '; with ({A: ' BBB '}) {function f () {alert ("F" + a);}; (function () {alert ("()" + a);}) (); The function is changed here f ();} Print first () bbb//then F aaa</script>
Print first () The outer of the BBB anonymous function points to the global environment with the outer of the with scope//and then the F -AAA f function.
Because, the scope of the F function is defined in the global.
It is important to note that the scope of function expressions and function declarations is different.
The function expression is created with the scope changed by with. Outer points to the WITH
function definition: Only about where the definition is. where it is. Without being affected by the with.
Try ... catch
var e = +; Try { e = +; The try scope is the global thrownew Error ();} Catch (e) { function f () { alert ("F" + e); }; (function () { alert ("()" + e); }) (); This changes the function scope f (); }
() Error self-executing function, scope is the scope in catch
F 20 global scope.
function expression of JS with Name
(function A () {
A = 111;
A = function () {
Alert (' B ');
};
alert (A);
})();
a=111 here; function cannot be rewritten
Alert (a) prints the data for the ToString of function A.
JS variable Scope