Brief introduction
Starting from this chapter, I will successively (translate, reprint, organize) http://dmitrysoshnikov.com/website about ECMAScript standard understanding of the Good article.
In this chapter we will explain the various types of execution contexts and associated executable code in the ECMAScript standard.
Original author: Dmitry A. Soshnikov
Original release: 2009-06-26
Russian Original: http://dmitrysoshnikov.com/ecmascript/ru-chapter-1-execution-contexts/
English translation: Dmitry A. Soshnikov.
Release time: 2010-03-11
English translation: http://dmitrysoshnikov.com/ecmascript/chapter-1-execution-contexts/
This article refers to the blog Park justinw Chinese translation, made some error correction, thanks to the translator.
Copy Code
Defined
Each time the controller goes to the ECMAScript executable code, it goes into an execution context. The execution context (referred to as-EC) is an abstract concept in the ECMA-262 standard that distinguishes it from the concept of executable (executable code).
The standard specification does not define the exact type and structure of the EC from the point of view of technology implementation, which should be a problem to be considered when implementing the ECMAScript engine concretely.
The active execution context group logically constitutes 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 ejected) when the EC type enters and exits the context.
Executable code type
The concept of the type of executable code is related to the abstract concept of execution context. At some point, the executable code and the execution context are entirely likely to be equivalent.
For example, we can define an execution context stack as an array:
Ecstack = [];
This stack is pressed every time a function is entered, even if the function is recursively called or as a constructor, or when the built-in Eval function works.
Global Code
This type of code is handled at the "program" level: for example, loading external JS files or code inside the local <script></script> tag. Global code does not include code in any function body.
In the initialization (program startup) phase, Ecstack is like this:
Copy Code code as follows:
Ecstack = [
Globalcontext
];
function code
When you enter the Funtion function code (all types of funtions), Ecstack is pressed into the new element. It should be noted that the specific function code does not include internal functions (inner functions) code. As shown below, we let the function adjust itself recursively:
Copy Code code as follows:
(function foo (bar) {
if (bar) {
Return
}
Foo (true);
})();
So, Ecstack is changed in the following way:
Copy Code code as follows:
Activation call for the first time Foo
Ecstack = [
<foo> Functioncontext
Globalcontext
];
Recursive activation invocation of Foo
Ecstack = [
<foo> functioncontext–recursively
<foo> Functioncontext
Globalcontext
];
Each return, it will exit the current execution context, the corresponding ecstack will pop up, the stack pointer will automatically move position, this is a typical stack implementation. A thrown exception is also possible to exit from one or more execution contexts if it is not intercepted. After the relevant code has been executed, the Ecstack will only contain the global context, all the time until the end of the application.
Eval Code
The eval code is kind of interesting. It has a concept: the invocation context (the calling context), such as the one generated when the Eval function is called. The eval (variable or function declaration) activity affects the invocation context (calling).
Copy Code code as follows:
Eval (' var x = 10 ');
(function foo () {
Eval (' var y = 20 ');
})();
alert (x); 10
alert (y); The "Y" hint is not declared
Ecstack Process of Change:
Copy Code code as follows:
Ecstack = [
Globalcontext
];
Eval (' var x = 10 ');
Ecstack.push (
Evalcontext,
Callingcontext:globalcontext
);
Eval exited context
Ecstack.pop ();
Foo Funciton call
Ecstack.push (<foo> functioncontext);
Eval (' var y = 20 ');
Ecstack.push (
Evalcontext,
Callingcontext: <foo> Functioncontext
);
Return from Eval
Ecstack.pop ();
Return from Foo
Ecstack.pop ();
That is, a very common logical call stack.
In the implementation of SpiderMonkey (Firefox,thunderbird) above version number 1.7, the invocation context can be passed to eval as the second argument. Then, if this context exists, it may affect the "private" (someone like to call it) a variable.
Copy Code code as follows:
function foo () {
var x = 1;
return function () {alert (x);};
};
var bar = foo ();
Bar (); 1
eval (' x = 2 ', bar); Incoming context, which affects the internal var x variable
Bar (); 2
Conclusion
This article is the basic theoretical basis for analyzing other topics related to execution contexts (such as variable objects, scope chains, and so on), which will be covered in a later chapter.
Other references