What is the difference and relationship between execution context and lexical environment?

前端 未结 3 752
故里飘歌
故里飘歌 2021-01-31 06:09

In JavaScript: Understanding the Weird Parts lexical environment is explained as the scope of your code while execution context is a collection of lexical environments, and that

3条回答
  •  走了就别回头了
    2021-01-31 06:55

    The best way to think of an execution context is as a stack frame, while lexical environments are indeed the scopes.

    The respective spec chapters (§8.1 Lexical Environments and §8.3 Execution Contexts) explain:

    • Execution contexts contain the current evaluation state of code, a reference to the code (function) itself, and possibly references to the current lexical environments.
      Execution contexts are managed in a stack.
    • Lexical environments contain an environment record in which the variables are stored, and a reference to their parent environment (if any).
      Lexical environments build a tree structure.

    With every change of the execution context, the lexical environment changes as well. However the lexical environment may change independently from that as well, for example when entering a block.

提交回复
热议问题