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:
- Callee-reference to the current function
- Length-Number of actually passed parameters
- 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:
- How should we understand the working principle of the JavaScript engine?
- JavaScript exploration: the importance of writing maintainable code
- JavaScript exploration: exercise caution when using global variables
- JavaScript exploration: var pre-parsing and side effects
- JavaScript exploration: for Loop (for Loops)
- JavaScript exploration: for-in loop (for-in Loops)
- Exploring JavaScript: Prototypes is too powerful
- JavaScript: eval () is the devil"
- JavaScript exploration: Using parseInt () for Numerical Conversion
- Exploring JavaScript: Basic coding specifications
- JavaScript exploration: function declaration and function expression
- JavaScript exploration: Name function expressions
- JavaScript: function name in the debugger
- JavaScript: JScript Bug
- JavaScript exploration: Memory Management of JScript
- Exploring JavaScript: SpiderMonkey's quirks
- JavaScript exploration: an alternative solution to naming function expressions
- JavaScript exploration: Object
- JavaScript exploration: Prototype chain
- JavaScript exploration: Constructor
- JavaScript probing: executable context Stack
- Execution context 1: Variable object and activity object
- Execution context 2: Scope chain Scope Chains
- Execution context 3: Closure Closures
- Execution context 4: This pointer
- Exploring JavaScript: Powerful prototype and prototype chain
- JavaScript Functions 1: function declaration
- JavaScript function 2: function expressions
- JavaScript function 3: function expressions in a group
- JavaScript function 4: function Constructor
- JavaScript variable object 1: VO Declaration
- JavaScript variable object 2: VO in different execution contexts
- JavaScript variable object 3: two stages of execution Context
- JavaScript variable object IV: Variables
- Property of the JavaScript variable object __parent _
- JavaScript scope chain 1: Scope chain Definition
- JavaScript scope chain 2: function Lifecycle
- JavaScript scope chain 3: Scope chain features
- JavaScript closure 1: Introduction to closures
- JavaScript closure 2: Implementation of closure
- JavaScript closure 3: Closure usage
Address of this article: http://www.nowamagic.net/librarys/veda/detail/1671,welcome.