[JavaScript] ECMA-262-3 in-depth analysis. Chapter 1. Execution Context

Source: Internet
Author: User
Tags types of functions
Introduction
Definition
Executable code
Global Code
Function Code
Eval code
Conclusion
Other references

This article mainly discusses the ECMAScript execution context and related ECMAScript executable code.

    Definition

    Each time the controller is transferred to the ECMAScript executable codeExecution Context.

    Execution context (-EC) is an abstract concept, which is used by ECMA-262 standards to distinguish it from executable code.

    The standard specification does not accurately define the EC type and structure from the perspective of technical implementation; this should be an issue to be considered when implementing the ECMAScript engine.

    The execution context of the activity is logically grouped into a stack. The bottom of the stack is always a global context (Global contextAt the top of the stack is the current (active) Execution context. The stack is modified when a variable of the EC type (varous kingds of EC) is pushed or popped up.

    Executable code

    The concept of executable code is relative to that of abstract execution context. At some point in time, executable code is equivalent to the execution context.

    For example, we can define an array to simulate the execution context Stack:

    ECStack = [];

    Every time you enter a function (even if the function is called recursively or used as a constructor) or the built-inEvalWhen the function is working, the stack is pushed.

    Global Code

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

    Looks like: "style =" background-color: # ffffff; "> in the initialization (Program startup) phase, ECStack is like this:

    Looks like: "style =" background-color: # ffffff; ">

    ECStack = [  globalContext];

    Function Code

    When function code (all types of Functions) is entered, ECStack is pushed into new elements. 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:

    // first activation of fooECStack = [  <foo> functionContext  globalContext]; // recursive activation of fooECStack = [  <foo> functionContext – recursively  <foo> functionContext  globalContext];

    Each time an existing execution context is returned and the corresponding execution context pops up on the ECStack, the stack pointer automatically moves the location. This is a typical stack implementation method. One thrown but not intercepted exception also has one or more execution contexts. After the relevant code segment is executed, the ECStack only includes the global context (Global context).

    Eval code

     EvalThe Code is a bit interesting. It has a concept:Calling context), This IsEvalContext generated when a function is called.Eval(Variable or function declaration) activities will affectCalling context).

    eval('var x = 10'); (function foo() {  eval('var y = 20');})(); alert(x); // 10alert(y); // "y" is not defined

    ECStack change process:

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

    As you can see, this 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, it may affect the variable "private" (similar to the variable named after the private keyword.

    function foo() {  var x = 1;  return function () { alert(x); };}; var bar = foo(); bar(); // 1 eval('x = 2', bar); // pass context, influence on internal var "x" bar(); // 2
    Conclusion

    The content of this article is the minimum theoretical basis for analyzing other topics related to execution context (such as variable objects, scope chains, and so on) in the future. These topics will be discussed in subsequent chapters.

    Other references

    The content of this article corresponds to chapter-10. Execution Contexts in the ECMA-262-3 Standard Specification.

     

    English address:ECMA-262-3 in detail. Chapter 1. Execution Contexts
    Chinese address: Http://www.cnblogs.com/justinw/archive/2010/04/16/1713086.html

    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.