In-depth understanding of the JavaScript series (11): Execution context (execution contexts)

Source: Internet
Author: User

Brief introduction

From the beginning of this chapter, I will continue (translation, reprint, collation) http://dmitrysoshnikov.com/website on the ECMAScript standard understanding of good text.

In this chapter we are going to explain the various types of execution contexts and related executable code in the ECMAScript standard.

original 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 date: 2010-03-11 English translation: http://  dmitrysoshnikov.com/ecmascript/chapter-1-execution-contexts/  This article refers to the blog Park Justinw's Chinese translation, made some error correction, thanks to the translator.
Defined

Each time the controller goes to the ECMAScript executable code, it goes into an execution context. The execution context (-EC) is an abstraction 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 considered when implementing the ECMAScript engine specifically.

The active execution context group logically composes 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 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 the execution context. At some point, the executable code is completely likely to be equivalent to the execution context.

For example, we can define an execution context stack as an array:

Ecstack = [];

This stack is pressed every time you enter function (even if function is called recursively or as a constructor) or when the built-in eval function works.

Global Code

This type of code is processed at the "program" level: for example, loading external JS files or code within a local <script></script> tag. The global code does not include any code in the function body.

During the initialization (program start) phase, the Ecstack is this:

Ecstack = [  globalcontext];
function Code

When entering the Funtion function code (all types of funtions), Ecstack is pressed into the new element. It is important to note that the specific function code does not include the intrinsic function (inner functions) code. As shown below, let the function self-tune its own way recursively:

(function  foo (bar) {  if (bar) {    return;  }  Foo (true);}) ();

Then, the ecstack is changed in the following way:

//  activation of the first foo call    ecstack = [<foo> functioncontext globalcontext]; //  foo" calls    ecstack = [<foo> functioncontext–recursively <foo> functioncontext Globalcontext]; 

Each time the return, will exit the current execution context, the corresponding ecstack will pop up, the stack pointer will automatically move the position, this is a typical way of stack implementation. A thrown exception can also be exited from one or more execution contexts if it is not intercepted. When the relevant code finishes executing, 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: Invoke context (calling context), for example, the context generated when the Eval function is called. The eval (variable or function declaration) activity affects the invocation context (calling context).

Eval (' var x = 10 '); (function foo () {  ///   // "y" hint no declaration

Ecstack Process of Change:

Ecstack = [Globalcontext];//eval (' var x = Ten ');Ecstack.push (Evalcontext, Callingcontext:globalcontext);//Eval exited contextEcstack.pop ();//foo Funciton callEcstack.push (<foo> functioncontext);//eval (' var y = x ');Ecstack.push (Evalcontext, Callingcontext: <foo> functioncontext);//return from EvalEcstack.pop ();//return from FooEcstack.pop ();

That is, a very common logical call stack.

In the implementation of SpiderMonkey (built into Firefox,thunderbird) with version number 1.7, you can pass the invocation context as the second parameter to eval. Then, if this context exists, it is possible to affect the "private" (someone likes to call it) variable.

functionFoo () {varx = 1;return function() {alert (x);};};varbar = foo (); Bar ();//1eval (' x = 2 ', bar);//The internal var x variable is affected by the incoming contextBar ();//2
Conclusion

This article is followed by an analysis of the minimum theoretical basis for other topics related to the execution of context, such as variable objects, scope chains, and so on, which will be covered in subsequent chapters.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

In-depth understanding of the JavaScript series (11): Execution context (execution contexts)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.