JS learn the scope chain of notes

Scope, the scope chain Explanations
Functions -> Function class object
Property accessible: including test.name test.prototype etc.
Inaccessible property: The test [[scope]]. Et
        ↓                               
                        Since the function execution resulting scope chain -> stored execution context of collection -> was chained
Javascript engine only access                                                    ↓
                                                  When each execution function, corresponding to a unique execution context of, i.e., destruction finished
 
(In a function) to find variables: from the top of the scope chain (the function), sequentially searches downward
 
Illustrates the scope chain
Note: In the following example, the definition of b is generated when a execution, a not performed, then it is not defined in b
 1  function a(){
 2      function b(){
 3         var b = 234;
 4       }
 5      var a = 123;
 6      b();
 7  }
 8         
 9  var glob = 100;
10  a();
When accessing the variable, it is looking at the scope chain execution
a defined: -> 0: GO when the global function is defined, to generate a global context
a execute: -> 0: AO when performing global function, generates its own unique execution context of, linked to the global context, the scope chain to form their own
                                1 : GO
 
b defined: -> 0: when the function is defined locally aAO, first to get the context of the existing external function
                                 1 : GO
b execute: -> 0: when performing a local function bAO, generate their own unique context of execution linked to the context of the external function has been previously got
                        -> 1: aAO
                        -->     2 : GO
 
 

 

B is defined inside a top, a direct successor of the scope chain (direct reference, point A is the original AO), and then, when executed, will generate their own AO linked to inherited scope chain

 

 

 

 

Guess you like

Origin www.cnblogs.com/seveinn/p/11801893.html