If the variable is related to the execution context, the variable should know where its data is stored and how to access it.
During JavaScript programming, we can't avoid declaring functions and variables to build our system successfully. But how and where can the interpreter find these functions and variables? What happened when we reference these objects?
Most ECMAScript programmers should know that variables are closely related to execution context:
Var a = 10; // variables in the Global Context (function () {var B = 20; // local variables in the function context}) (); alert (); // 10 alert (B); // The global variable "B" is not declared
In addition, many programmers know that the current ECMAScript specification specifies that the independent scope can only be created through the execution context of the "function" code type. That is to say, compared with C/C ++, The for loop in ECMAScript cannot create a local context.
For (var k in {a: 1, B: 2}) {alert (k);} alert (k); // although the loop has ended, the variable k is still in the current scope
Let's take a look at the details we found when declaring the data.
Data Declaration
If the variable is related to the execution context, the variable should know where its data is stored and how to access it. This mechanism is called a variable object ).
A variable object (VO) is a special object related to the execution context. It stores the following content declared in the context:
- Variable (var, variable Declaration );
- Function declaration (FunctionDeclaration );
- Function Parameters
For example, we can use a common ECMAScript object to represent a variable object:
VO = {};
As we said, VO is the property of the execution context ):
ActiveExecutionContext = {VO: {// Context Data (var, FD, function arguments )}};
Only the variable objects in the global context can be indirectly accessed through the VO attribute name (because in the global context, the global object itself is a variable object, which will be detailed later ), in other contexts, you cannot directly access the VO object because it is only an implementation of the internal mechanism.
When we declare a variable or function, it is no different from when we create a new VO attribute (that is, there are names and corresponding values ). For example:
var a = 10; function test(x) { var b = 20;}; test(30);
The corresponding variable object is:
// Global context variable object VO (globalContext) = {a: 10, test:
}; // Test function context variable object VO (test functionContext) = {x: 30, B: 20 };
Variable objects are just an abstract concept at the specific implementation level (and in specifications. (Essentially, in the context of specific execution, the VO name is different and the initial structure is different.
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
This article is available at http://www.nowamagic.net/librarys/veda/detail/1670.