0

I am reading a tutorial about closure and it describes Execution Context as follows:

When a JavaScript function is executed, a construct referred to as an Execution Context is created. The Execution Context is an abstract concept prescribed by the specification to track the execution progress of its associated code. As an application runs, an initial Global Execution Context is created. As each new function is created, new Execution Contexts are created which form an Execution Context stack.

When a JavaScript function is executed, a construct referred to as an Execution Context is created.

This sentence says that an Execution Context is created when a function is executed.

As each new function is created, new Execution Contexts are created which form an Execution Context stack.

This sentence says that an Execution Context is created when a new function is created.

My question is: which one is right?

ChenLee
  • 1,371
  • 3
  • 15
  • 33

1 Answers1

4

The first one. Execution contexts are created when functions are executed. The "execution context stack" is also known as "call stack".

When functions are created, they store a reference to the scope of the active execution context (that created them) - this is what we call a closure scope. When they will be executed, their execution context will be created with a new scope that is linked to the stored one - forming a scope chain.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Execution contexts are created when functions are executed.When function are created the execution context has not been created and how does it store a reference to the scope of the current execution context? – ChenLee Mar 22 '17 at 03:12
  • 1
    The function gets the reference to the scope, not the (indeed not yet created) execution contexts. It takes the reference from the current execution context, whose execution led to the function being created. – Bergi Mar 22 '17 at 03:32
  • @Bergi, when is a function created? – user51462 May 29 '21 at 11:34
  • 1
    @user51462 When the scope where it is declared is entered, or when the expression defining it is evaluated. – Bergi May 29 '21 at 11:35
  • @Bergi, a lot of articles on the execution context (EC) split the concept up into a "creation phase" and an "execution/activation phase" (for example, see [here](http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/#function-life-cycle) and [here](https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c)). I'm not sure if this is the correct way to think about ECs (since there is no mention of these phases in the current or past specs), but when you say "function is created", are you referring to the so-called "creation phase" of an EC? – user51462 May 30 '21 at 06:37
  • Also, is it correct to say that this "creation phase" takes place during compilation (hence, "lexical" scope), whereas the "execution phase" takes place during the interpretation/execution of the program? I can't find any resources that talk about how actual implementations like V8 use the abstract concepts defined in the spec, so it still feels like a blackbox to me. – user51462 May 30 '21 at 06:40
  • 1
    @user51462 *Entering the scope* means *creating the scope*. Some functions are created in the creation phase, some in the execution phase. And no, this has nothing to do with compilation. You'll want to read [this answer](https://stackoverflow.com/a/64592696/1048572). – Bergi May 30 '21 at 09:59
  • hello @Bergi thank you. So, an execution context only has **"direct"** access to the parent lexical environment (and obviously its own) and the **other more external lexical environments** it has access to in turn are already **thanks to the scope chain** (i.e. because the parent lexical environment **in turn accesses its parent's lexical environment and so on**), right? +1 –  Jan 28 '23 at 01:59
  • 1
    @Daniel The execution context *only* has its own (currently active) lexical environment, nothing else. Everything else is accessed through the scope chain (i.e. the parent, "outer" lexical environment). – Bergi Jan 28 '23 at 02:11
  • 1
    @Daniel (Admittedly, it also has access to it's active function which knows which environment it closed over, but that is irrelevant for resolution of identifiers) – Bergi Jan 28 '23 at 02:14
  • "their execution context will be created with a new scope that is linked to the stored one - forming a scope chain" So, do you mean to say that the currently running execution context has a "**binding-link**" to the parent lexical environment? @Bergi –  Jan 28 '23 at 02:29
  • @Daniel I don't know what you mean by "binding-link". Also, parent of what? – Bergi Jan 28 '23 at 02:34
  • @Bergi I have seen in many places that scopes are connected so that closures look for variables in more external scopes (if the variable is not found in the current scope), something like prototype chain, that's why I say so. –  Jan 28 '23 at 02:39
  • 1
    Yes, scopes (lexical environments) are connected by an internal property called *[[OuterEnv]]*. They form a scope chain, and other than "outer" you can also use the term "parent scope / lexical environment". But this has nothing to do with execution contexts. – Bergi Jan 28 '23 at 02:43
  • great, so, I was mixing things up, the lexical environments are the ones that are actually connected, I was confused, thanks @Bergi, I've learned a LOT WITH YOU, thanks for your patience –  Jan 28 '23 at 02:49
  • 1
    "it also has access to it's active function which knows which environment it closed over, but that is irrelevant for resolution of identifiers)", that's `[[Environment]]`, right?@Bergi –  Jan 28 '23 at 04:16
  • 2
    @GeorgeMeijer [Yes](https://262.ecma-international.org/13.0/#sec-ecmascript-function-objects) – Bergi Jan 28 '23 at 04:36
  • Thanks @Bergi, according to your explanation, the execution context refers **only** to the actually **active** execution context, that's why it only has a lexical environment (which is **the active one** corresponding to that execution context), i.e. "execution context" is just to "emphasize" that it is **the code** being executed in a specific moment isn't it? –  Jan 28 '23 at 15:57
  • 2
    @GeorgeMeijer Execution contexts form a stack (the top one is the active, currently running one), each of them has its own lexical environment that is used when they are running. – Bergi Jan 28 '23 at 16:06
  • @Bergi, [[outerEnv]] and [[Environment]] are the same, the outer lexical environment, right? –  Feb 05 '23 at 03:38
  • 1
    @Coder23 They're internal slots of different things – Bergi Feb 05 '23 at 04:09