JavaScript variable object 2: VO in different execution contexts

Source: Internet
Author: User
For all types of execution contexts, some operations (such as variable initialization) and actions of variable objects are the same variable objects in different execution contexts.

For all types of execution contexts, some operations (such as variable initialization) and actions of the variable object are common. From this perspective, it is easier to understand the variable object as an abstract basic thing. The additional content related to the variable object is also defined in the function context.

Abstract variable object VO (general behavior of variable initialization) extends> global context variable object GlobalContextVO extends (VO === this === global) function context variable object FunctionContextVO (VO = AO, and
 
  
)
 

Let's take a closer look:

Variable objects in the Global Context

First, we need to give a clear definition to the global object:

A Global object is an object that has been created before any execution context is entered. This object only has one copy, and its attributes can be accessed anywhere in the program. The life cycle of the global object ends at the moment when the program exits.

The Math, String, Date, and parseInt attributes of the global object are initialized in the initial creation phase, you can also create additional objects as attributes (which can point to the global object itself ). For example, in DOM, the window attribute of the global object can reference the global object itself (Of course, not all implementations are like this ):

Global = {Math: <...>, String: <...>... window: global // reference itself };

Prefix is often ignored when accessing global object attributes, because global objects cannot be accessed directly by name. However, we can still access global objects through this in the global context. We can also reference ourselves recursively. For example, window in DOM. To sum up, the code can be abbreviated:

String (10); // global. string (10); // window with the prefix. a = 10; // = global. window. a = 10 = global. a = 10; this. B = 20; // global. B = 20;

Therefore, return to the variable object in the Global Context -- here, the variable object is the global object itself:

VO(globalContext) === global;

It is very necessary to understand the above conclusion. Based on this principle, we can access it indirectly through the attributes of the global object in the global context. (For example, do not know the variable name beforehand ).

Var a = new String ('test'); alert (a); // direct access, found in VO (globalContext: "test" alert (window ['a']); // indirectly through global access: global === VO (globalContext): "test" alert (a === this. a); // true var aKey = 'a'; alert (window [aKey]); // access by dynamic attribute name indirectly: "test"
Variable objects in the function Context

In the context of function execution, VO cannot be directly accessed. At this time, the active object (AO) plays the role of VO.

VO(functionContext) === AO;

An active object is created when the context of the function is entered. It is initialized through the arguments attribute of the function. The value of the arguments attribute is an Arguments object:

AO = {  arguments: };

An Arguments object is an attribute of an activity object. It includes the following attributes:

  1. Callee-reference to the current function
  2. Length-Number of actually passed parameters
  3. The properties-indexes (an integer of the string type) attribute value is the parameter value of the function (arranged from left to right by the parameter list ). The number of elements in properties-indexes is equal to that in arguments. length. properties-indexes and the actually passed parameters are shared.

For example:

Function foo (x, y, z) {// number of declared function parameters arguments (x, y, z) alert (foo. length); // 3 // The number of actually passed parameters (only x, y) alert (arguments. length); // 2 // The callee parameter is the alert (arguments. callee = foo); // true // The parameter shares alert (x = arguments [0]); // true alert (x ); // 10 arguments [0] = 20; alert (x); // 20 x = 30; alert (arguments [0]); // 30 // However, the Parameter z that is not passed in is not shared with the 3rd index values of the parameter z = 40; alert (arguments [2]); // undefined arguments [2] = 50; alert (z); // 40} foo (10, 20 );

The code in this example has a bug in the current Google Chrome browser-even if the z, z, and arguments [2] parameters are not passed, they are still shared.

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

Address of this article: http://www.nowamagic.net/librarys/veda/detail/1671,welcome.

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.