JavaScript's execution context stack

Source: Internet
Author: User

Execution context Stack

There are three types of ECMAScript code: Global Code, function code, and eval code. The code executes in its execution context.

There is a unique global context, and there may be multiple functions and the eval context. Each function call goes to the execution of the function

Below, the code that executes the function. When called to the Eval function, enter into the eval execution context and execute its code.

Notice that a function may produce a finite set of contexts, because each call to a function (or even its own invocation) produces a

New context, accompanied by a new context state:

function Foo (bar) {} // call the same function // generates three different contexts, each time it is called // at the same time by the different upper and lower states (bar parameter value is different)foo (ten); foo (30);

A context can be used to activate another context called the caller. A context is being activated called the callee. Be adjusted

At the same time, the user may become the caller of another callee (for example, a function is called by the global context and it

Called an intrinsic function).

When a caller activates (invokes) the callee, the caller suspends its context and transfers the control flow to the

Called by the caller. The callee is pushed into the stack and becomes the executing context. After the callee finishes, it transfers the control

To the callee, then executes the callee context procedure (it may activate another context) until it ends, and so on.

The callee may simply return or exit unexpectedly. Throws an exception without capturing one or more upper and lower possible exits (removing the stack)

Text

When all the ECMAScript programs run, the execution stack, such as the top of the stack, is the current execution context:

When the program starts executing, it goes into the global execution context, which is at the bottom of the stack and the first element of the stack.

In addition, the global code is initialized to create the necessary objects and functions. When executed in the global context, its code may be activated

(already created) function, this function will go into their execution context, press the new element onto the stack, and in turn the class

Push. After this initialization is completed, the running system waits for certain events (for example, a user's click) to activate

function, and enters into a new execution context.

In the diagram below, there is a function context called EC1, and global EC, which we have the following process stack modifications

EC1 of the stack and the stack

This is a clear explanation of how the ECMAScript system manages the code of execution when it is run.

More information about the execution context in the next section, the execution context.

As we said, each execution context may be similar to an object in the stack. Let's look at its structure and what kind of state (which

Properties) A context is required to execute its code.

JavaScript's execution context stack

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.