JavaScript: Execution context (Execution Contexts)

Source: Internet
Author: User

JavaScript: Execution context (Execution Contexts)
Introduction

 

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

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:

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:

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

Then, ECStack is changed as follows:

// Call ECStack for the first foo activation = [
 
  
FunctionContext globalContext]; // call ECStack for Recursive activation of foo = [
  
   
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 ).

Eval ('var x = 10'); (function foo () {eval ('var y = 20') ;}) (); alert (x ); // 10 alert (y); // y indicates no declaration

ECStack change process:

ECStack = [  globalContext]; // eval('var x = 10');ECStack.push(  evalContext,  callingContext: globalContext); // eval exited contextECStack.pop(); // foo funciton callECStack.push(
 
   functionContext); // eval('var y = 20');ECStack.push(  evalContext,  callingContext: 
  
    functionContext); // return from evalECStack.pop(); // return from fooECStack.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.

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.

 

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.