Deep understanding of the JavaScript series (11) Execution context (ExecutionContexts) _ javascript skills

Source: Internet
Author: User
This chapter describes the execution context in the ECMAScript standard and various types of executable code.
From the beginning of this chapter, I will continue to (translate, repost, and organize) http://dmitrysoshnikov.com/the website is about the excellent documents of the ecmascriptstandard.

This chapter describes the execution context in the ECMAScript standard and various types of executable code.

Author: Dmitry A. Soshnikov
Original release:
Original Russian: http://dmitrysoshnikov.com/ecmascript/ru-chapter-1-execution-contexts/

Dmitry A. Soshnikov.
Release date: 2010-03-11
Http://dmitrysoshnikov.com/ecmascript/chapter-1-execution-contexts/

In this article, I have referred to justinw's Chinese translation and made some mistakes. Thank you for your help.

Definition
Each time the controller is transferred to ECMAScript executable code, it enters an execution context. Execution context (-EC) is an abstract concept in the ECMA-262 standard, used to distinguish from the executable code concept.

The standard specification does not define the exact type and structure of the EC from the perspective of technical implementation, which should be considered in the specific implementation of the ECMAScript engine.

The execution context Group of the activity is logically grouped into a stack. The bottom of the stack is always a global context, and the top is the current (active) Execution context. The stack is modified (pushed or popped up) when the EC type enters and exits the context ).

Executable code type
The type of executable code is related to the abstract concept of execution context. At some point in time, the executable code and the execution context may be equivalent.

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

ECStack = [];
This stack is pushed into a function every time it enters the function (even if the function is recursively called or used 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 in the Local "script" "script" label. The Global Code does not include any code in the function body.

In the initialization (Program startup) phase, ECStack is like this:

The Code is as follows:


ECStack = [
GlobalContext
];


Function Code
When you enter the funtion Function Code (all types of funtions), The ECStack is pushed into the new element. Note that the specific function code does not include the inner functions code. As shown in the following figure, let the function call its own method for recursion:

The Code is as follows:


(Function foo (bar ){
If (bar ){
Return;
}
Foo (true );
})();


Then, ECStack is changed as follows:

The Code is as follows:


// The first foo activation call
ECStack = [
FunctionContext
GlobalContext
];

// Call the recursive activation of foo
ECStack = [
FunctionContext-recursively
FunctionContext
GlobalContext
];


Each return operation exits the current execution context. The corresponding ECStack will pop up and the stack pointer will automatically move the position. This is a typical stack implementation method. If a thrown exception is not intercepted, it may also exit from one or more execution contexts. After the related code is executed, ECStack only contains the global context until the end of the entire application.

Eval code
The eval code is a bit interesting. It has a concept: calling context, for example, the context generated when the eval function is called. The eval (variable or function declaration) activity affects the call context ).

The Code is as follows:


Eval ('var x = 10 ');

(Function foo (){
Eval ('var y = 20 ');
})();

Alert (x); // 10
Alert (y); // "y" prompts no declaration


ECStack change process:

The Code is as follows:


ECStack = [
GlobalContext
];

// Eval ('var x = 10 ');
ECStack. push (
EvalContext,
CallingContext: globalContext
);

// Eval exited context
ECStack. pop ();

// Foo funciton call
ECStack. push ( FunctionContext );

// Eval ('var y = 20 ');
ECStack. push (
EvalContext,
CallingContext: FunctionContext
);

// Return from eval
ECStack. pop ();

// Return from foo
ECStack. pop ();


That is, a very common logical call stack.

In the implementation of SpiderMonkey (embedded in Firefox, Thunderbird) with version 1.7 or later, you can pass the call context to eval as the second parameter. If the context exists, the variable "private" may be affected.

The Code is as follows:


Function foo (){
Var x = 1;
Return function () {alert (x );};
};

Var bar = foo ();

Bar (); // 1

Eval ('X = 2', bar); // The Input Context affects the internal var x variable.

Bar (); // 2


Conclusion
This article is the minimum theoretical basis for analyzing other theme related to execution context (such as variable objects, scope chains, and so on). These theme will be discussed in subsequent chapters.

Other references

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.