A lexical environment maps identifiers to variables. When a function is created, JavaScript stores a reference to its creation-time lexical environment in the internal [[Environment]] property. This lets the function find outer variables even when it is called elsewhere. section 5.4 section 5.4.2
When the function is called:
- JavaScript creates a new execution context and lexical environment for that call. section 5.4.2
- The new environment links outward to the environment referenced by the function’s [[Environment]] property. Identifier lookup can then reach variables in outer environments. section 5.4.2
Trace how JavaScript resolves variable values through nested environments. section 5.4.2
