JS advanced-Execution context and scope chain

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.