0

Where in the ECMAScript specification can we find text that describes exactly when a new Lexical Environment is created?

  • I could not find it in "8.1 Lexical Environments", which just loosely states:

    Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a BlockStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated.

  • From "8.3 Execution Contexts", we know every execution context contains a Lexical Environment, but we do not know exactly when new execution contexts are created, and indeed if there are other situations where Lexical Environments are created. 8.3 only states:

    New execution context is created whenever control is transferred from the executable code associated with the currently running execution context to executable code that is not associated with that execution context.

I'm trying to understand the behavior of JavaScript implementations, solely based on information from the official ECMAScript specification. Thus, I try to avoid any terms not defined therein (such as scope).

Magnus
  • 6,791
  • 8
  • 53
  • 84

1 Answers1

2

8.1.2.2 - 8.1.2.6 are the ways that a new lexical environment is created. You can find all the places those are used in the spec by searching for the name.

Eg. if you search for NewFunctionEnvironment, you will find that one is created in 9.2.1.1 PrepareForOrdinaryCall. There is only one place in the spec where each of Global, Module, Function, and Object environments are created. There are several places where Declarative environments are created.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • Got it, thank you, good idea. I am ultimately trying to nail down the concept of *scope* and `this`, based on the spec and the terms used therein. It seems, since *scope* means visibility of variables/functions, *scope* is just the *Lexical Environment* of the *running execution context*. MDN seems to define `this` in relation to a *Global Execution Context* and a *Function Context*. I will try to tie that in to the spec as well. Will probably do another question on that though. – Magnus Jul 26 '18 at 17:13
  • @Magnus *scope* is lexical (and therefore static, determined at parse time), and `this` is dynamic, determined at invocation time. – Pointy Jul 26 '18 at 17:16