Home > Article > Web Front-end > In-depth understanding of JavaScript series (11) Execution Contexts_javascript skills
Introduction
Starting from this chapter, I will continue (translate, reprint, organize) http://dmitrysoshnikov.com/ website’s good articles on understanding ECMAScript standards.
What we are going to explain in this chapter is the execution context and various types of related executable code in the ECMAScript standard.
Original author: Dmitry A. Soshnikov
Original release: 2009-06-26
Original Russian text: http://dmitrysoshnikov.com/ecmascript/ru-chapter-1-execution-contexts /
English Translation: Dmitry A. Soshnikov.
Published: 2010-03-11
English Translation: http://dmitrysoshnikov.com/ecmascript/chapter-1-execution-contexts/
This article refers to the Chinese translation of blog garden justinw and made some error corrections. Thanks to the translator.
Copy code
Definition
Every time the controller switches to ECMAScript executable code, it enters an execution context. Execution context (-EC for short) is an abstract concept in the ECMA-262 standard, used to distinguish it from the concept of executable code.
The standard specification does not define the exact type and structure of EC from a technical implementation perspective. This should be an issue to be considered when specifically implementing the ECMAScript engine.
Active execution context groups logically form a stack. The bottom of the stack is always the global context, and the top is the current (active) execution context. The stack is modified (pushed or popped) when EC types enter and exit the context.
Executable code type
The concept of executable code type is related to the abstract concept of execution context. At some point, it is entirely possible that executable code and execution context are equivalent.
For example, we can define the execution context stack to be an array:
ECStack = [];
Every time a function is entered (even if the function is called recursively or as a constructor) or When the built-in eval function works, this stack will be pushed.
Global code
This type of code is handled at the "program" level: for example, loading external js files or code within local <script></script> tags. Global code does not include any code within a function body.
In the initialization (program startup) phase, ECStack looks like this: