JavaScript probing: executable context Stack

Source: Internet
Author: User
There are three types of code in ECMASscript: global, function, and ev.

Execution Context Stack ).

There are three types of code in ECMASscript: global, function, and eval.

The execution of each type of code depends on its own context. Of course, the global context may cover a lot of function and eval instances. Every call to a function enters the context of the function execution and calculates the values of variables in the function. Each execution of the eval function also enters the context of the eval execution to determine where to obtain the value of the variable.

Note that a function may generate an infinite context, because a function call (or even recursion) produces a new context.

Function foo (bar) {}// call the same function, three different contexts are generated each time. // (contains different states, such as the value of the bar parameter) foo (10); foo (20); foo (30 );

An execution context can activate another context, just like a function calls another function (or a global context calls a global function), and then calls the function layer by layer. Logically speaking, this implementation method is stack, which we can call as the context stack.

A context that activates other contexts is called a caller ). The activated context is called callee ). The caller may also be the caller (for example, a function called in a global context calls some internal methods ).

When a caller activates a callee, the caller suspends its own execution and gives control to the callee. therefore, this callee is put into the stack and is called the context in progress [running/active execution context]. after the callee context ends, the control is handed over to its caller again, and then the caller will continue to execute in the paused place. After the caller ends, other contexts will be triggered. A callee can end its context by returning or throwing an exception.

For example, the execution of all ECMAScript programs can be considered as an execution context stack [execution context (EC) stack]. The top of the stack is the active context.

Execution context Stack

When a program starts, it first enters the global execution context Environment [global execution context], which is also the lowest element in the stack. The global program starts initialization to generate the necessary objects [objects] and functions [functions]. in the global context execution process, it may activate some methods (of course initialized), then enter their context environment, and then push new elements into the stack. After these initialization ends, the system will wait for some events (such as users' mouse clicks), trigger some methods, and then enter a new context.

There is a function context "EC1" and a Global context "Global EC", showing the stack changes when "Global EC" enters and exits "EC1:

Changes in execution context Stack

When ECMAScript is running, the system manages code execution in this way. As mentioned above, each execution context in the stack can be expressed as an object. Let's take a look at the structure of the context object and the state required to execute its code ).

Additional reading

The topic list of this article is as follows:

  1. How should we understand the working principle of the JavaScript engine?
  2. JavaScript exploration: the importance of writing maintainable code
  3. JavaScript exploration: exercise caution when using global variables
  4. JavaScript exploration: var pre-parsing and side effects
  5. JavaScript exploration: for Loop (for Loops)
  6. JavaScript exploration: for-in loop (for-in Loops)
  7. Exploring JavaScript: Prototypes is too powerful
  8. JavaScript: eval () is the devil"
  9. JavaScript exploration: Using parseInt () for Numerical Conversion
  10. Exploring JavaScript: Basic coding specifications
  11. JavaScript exploration: function declaration and function expression
  12. JavaScript exploration: Name function expressions
  13. JavaScript: function name in the debugger
  14. JavaScript: JScript Bug
  15. JavaScript exploration: Memory Management of JScript
  16. Exploring JavaScript: SpiderMonkey's quirks
  17. JavaScript exploration: an alternative solution to naming function expressions
  18. JavaScript exploration: Object
  19. JavaScript exploration: Prototype chain
  20. JavaScript exploration: Constructor
  21. JavaScript probing: executable context Stack
  22. Execution context 1: Variable object and activity object
  23. Execution context 2: Scope chain Scope Chains
  24. Execution context 3: Closure Closures
  25. Execution context 4: This pointer
  26. Exploring JavaScript: Powerful prototype and prototype chain
  27. JavaScript Functions 1: function declaration
  28. JavaScript function 2: function expressions
  29. JavaScript function 3: function expressions in a group
  30. JavaScript function 4: function Constructor
  31. JavaScript variable object 1: VO Declaration
  32. JavaScript variable object 2: VO in different execution contexts
  33. JavaScript variable object 3: two stages of execution Context
  34. JavaScript variable object IV: Variables
  35. Property of the JavaScript variable object __parent _
  36. JavaScript scope chain 1: Scope chain Definition
  37. JavaScript scope chain 2: function Lifecycle
  38. JavaScript scope chain 3: Scope chain features
  39. JavaScript closure 1: Introduction to closures
  40. JavaScript closure 2: Implementation of closure
  41. JavaScript closure 3: Closure usage

This article is available at http://www.nowamagic.net/librarys/veda/detail/1643.

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.