JavaScript scope and context

Scope when the function definition has been determined, the execution context is determined only when the function call. In the global scope and function scope is created execution context (when there is a closure, a scope there are two context is there). Each call to the function will generate a new execution context. But active execution context is only one, it is a push of a process stack.

Execution Context: The function is called once each, will generate a new execution context, because different call may have different parameters.

A = the let 10 , fn,        // . 1, into the global context 
     bar = function (X) { 
         the let B = . 5 
         fn (X + B)      // . 3, into the context function fn 
     } 
fn = function (Y) { 
    the let C = . 5 
    the console.log (Y + C) 
} 

bar ( 10 )                  // 2, into the context function bar

First (Comment number 1), prior to the implementation of the code, first create a global context (active) as follows:

// global context 
A: undefined 
the Fn: undefined 
bar: undefined 
the this : window

After the start executing code, the code prior to the line 10, in the context of the following variables are assigned during execution:

// global context 
A: 10 
Fn: function 
bar: function 
the this : window

Then perform the bar (10), the function jumps to the inner bar, before executing the function body statement in the function bar creates a new execution context as follows, and the push bar execution context.

// bar execution context 
B: undefined 
X: 10 
arguments: [ 10 ]
 the this : window

3 comments to digital and then perform there, calling fn function, before executing the function body statement creates a new execution context as follows, and fn execution context onto the stack.

// Fn execution context 
C: undefined 
Y: 15 
arguments: [ 15 ]
 the this : window

In the upper step, fn after the execution completes, fn fn function generates a context stack, is destroyed. Then the bar is finished, call the context bar function generates a stack is destroyed. Then the rest of the global context, the stack destroyed. FIG following steps:

Scope: In JavaScript In addition to the global scope of each function also as a scope, outside the scope can not access variables inside the scope.

The scope chain: If you want to get have a variable value, first look at the current scope, if not, it will seek to outer scope, no scope to find in the outer layer, it is found over, it reported the anomaly can not find, on the formation of the scope chain.

Each function of a scope, if the function nested function appears, the scope chain will occur. JavaScript scope has been created before being executed, only in accordance with the scope chain to look for when you can and then go after the execution.

Free variables: such variables a, is used in the fn function scope, but not in fn scope statement, but in other scopes statement, this is a free variable.

 

Guess you like

Origin www.cnblogs.com/xjy20170907/p/11423292.html