Execution context 1: Variable object and activity object

Source: Internet
Author: User
VariableObject is an abstract concept. It represents

The context of an execution can be abstracted as an object. Each execution context has a series of attributes (called context state) that are used to track the execution progress of associated code. This graph is a context structure.

Context Structure

In addition to the three required attributes (variable object), this pointer (this value), scope chain )), the execution context can also have any additional attributes based on the specific implementation. Next, let's take a closer look at these three attributes.

Variable Object)

A variable object is a scope of data related with the execution context.
It's a special object associated with the context and which stores variables and function declarations are being defined within the context.

Variable object is the scope of data related to the execution context ).
It is a special object associated with the context, used to store variables and function declarations defined in the context ).

Variable Object is an abstract concept. Different contexts use different objects. For example, in the global context, the variable object is also the global object itself [global object]. (This means that we can point to global variables through the Global Object Attributes ).

Let's take a look at the global execution context in the following example:

Var foo = 10; function bar () {}/// function declaration (function baz () {}); // function expression console. log (this. foo = foo, // true window. bar = bar // true); console. log (baz); // reference error. baz is not defined

The variable object (VO) in the global context has the following attributes:

Global variable object

As shown above, the function "baz" is not included in variable objects if it is used as a function expression. This is the reason why a ReferenceError occurs when a function tries to access the service. Note that, compared with other languages (such as C/C ++), ECMAScript only allows functions to create new scopes. Variables and internal functions defined in the function are not directly visible outside and do not pollute global objects. When using eval, we will also use a new (eval creation) Execution context. Eval uses the global variable object or the caller's variable object (the call source of eval ).

What is the function and its own variable object? In a function context, a variable object is represented as an activity object ).

Activation object)

When the function is activated by the caller, this special activity object is created. It contains common parameters and special parameters (arguments) objects (parameter ing tables with index attributes ). Activity objects are used as variable objects in the context of functions.

That is, the variable object of the function remains unchanged, but besides the stored variables and function declaration, it also contains the special object arguments. Consider the following:

Function foo (x, y) {var z = 30; function bar () {}// function declaration (function baz (){}); // function expression} foo (10, 20 );

Shows the next activation object (AO) in the context of the "foo" function:

Activation object

In the same way, function expressions are not in the ranks of AO.

Next we will talk about the third main object. As we all know, in ECMAScript, we will use the internal function [inner functions]. In these internal functions, we may reference its parent function variable or global variable. We turn these variable objects into the context scope object [scope object of the context]. Similar to the prototype chain [prototype chain] discussed above, we are called the scope chain [scope chain] Here.

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/1644.

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.