Js variable object scope chain
Executable code: Global, function, eval
Context type created during code execution: global context, function context, and eval Context
Execution context (EC) attributes: Variable object (vo), this, scope chain (SC)
Execution context Stack
Variable object
Used for storage: variables, declarative functions, and parameters
Global Object: variables and declarative functions all exist in the window object.
Activated Object: Arguments Object
Callee length [0]... [Length-1]
Variable instantiation
Priority: declarative function> function parameter> variable
(Variables added during instantiation cannot be deleted)
Var a = 10; delate // window. a cannot be deleted; delate // window. a can be deleted
Code execution steps: Enter the context
Initialize this scope chain variable object variable instantiate code execution scope chain
The scope chain can be seen as an array.
Global context scope chain [Global Object] function context scope chain [currently activated object + function. [[scope]
With can temporarily change the scope chain, because more than once the variable is searched, may lead to lower efficiency.
Use an example to describe the Code Execution Process
EC: Execution Context
VO: variable objec variable object
SC: scope chain
AO: activation object
var y = 10;function test(){ var y = 2; return function(){ alert(y); }}var fun = test();fun();
Create context stage
Initialize this, variable object, scope chain, and variable instantiation
``` jsglobal EC={ VO: { test : test_fun_objcet, y : undefined, fun : undefined }, this : window, SC : [window]}```
Context Stack
Code execution stage <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4ncjxwcmugy2xhc3m9 "brush: java;"> global EC={ VO: { test : test_fun_objcet, y : 10, fun : test_fun_objcet }, this : window, SC : [window]}
Execute fun (), that is, the test function,
It also enters the context of test, initializes this, variable object, scope chain, and variable instantiation.
Test EC = {VO: {y: undefined}, this: window, SC: [testAO, window] // SC = [currently activated object + function. [[scope] // The activation object is testAO // function. [[scope] is the SC where the function is created, that is, [window]}
Context Stack
Test stage
test EC={ VO: { y : 2 }, this : window, SC :[testAO,window]}
Returns an anonymous function,
Context for creating an anonymous (anonymous) Function
Anonymous EC = {VO :{}, this: window, SC: [anonymousAO, testAO, window] // SC = [currently activated object + function. [[scope] // The activation object is anonymousAO // function. [[scope] is the SC where the function is created, that is, [testAO, window]}
Context Stack
This anonymous function does not have declarative functions, parameters, and variables, so it enters the execution stage.
Alert (y), search for y in the current SC, and find y in the current SC = [anonymousAO, testAO, window]. There is no y in anonymousAO, and y in testAO is 2, so the pop-up result is 2.End