This problem involves three points:
1. Execution context
2. Execution context stack resulting from function nesting
3. Closures
1<script>2 varA = 1;3 varF1 =function(){//First function4 varA = 2;5 varb = 1;6 7 varF2 =function(){//a second function8 varc = 1;
9 varF3 =function() {//Third function
//The third function executes, without a,b,c in his own execution context, from the execution context of the parent function F2, with C in F2, but without a B,
Go to F2 's parent function F1 's execution context to find a B, so the result is 2,1,1Ten Console.log (a,b,c);//2,1,1 One } A f3 (); - } - F2 (); the Console.log (A, b);//2,1 - Console.log (c);//error: C Not defined - } - F1 ();
F1 execution, which produces the execution context, has a B in the execution context so Console.log (A, A, a) is 2,1
But there is no C, so go to F1 's parent function (Global execution context) to find, or no C, then Console.log (c) Error
Show execution context content in the order of the stack
---------------------F3 function Execution Context------------------------------
Function
-----------------------------------------------------------------
---------------------F2 function Execution Context------------------------------
Variable: c = 1;
function: f3;
-----------------------------------------------------------------
---------------------F1 function Execution Context------------------------------
Variable: a = 2 b = 1;
function: F2;
------------------------------------------------------------------
---------------------the global execution context---------------------------------
Variable: a = 1
function F1;
------------------------------------------------------------------</script>
Concept:
1. The Global Code (the code that executes when the page is opened) is executed, resulting in a "global execution context" with only one
2. Once the function code is executed, it produces a "function execution context" that can have multiple
3. The "variables" and "functions" in the code are stored in the context of the execution; they are the raw material for code execution.
4. The execution context provides the values of the individual data required for the code to run
5. function Nesting calls form the execution context stack
JS advanced-Execution context and scope chain