Introduction
In a program, we always declare variables and functions, and then successfully use them to build our system. When we reference the desired object, how does the interpreter find our data (functions, variable?
Many ECMAScript programmers know that variables are closely related to execution context.
var a = 10; // variable of the global context(function () { var b = 20; // local variable of the function context})();alert(a); // 10alert(b); // "b" is not defined
At the same time, many programmers know that in the current version specification, independent scopes can only be created through the execution context of the "function" code type. That is to say, compared with C/C ++, the circular block in ECMAScript cannot create a local context.
for (var k in {a: 1, b: 2}) { alert(k);}alert(k); // variable "k" still in scope even the loop is finished
What happens when data is declared? Let's take a look at more details.
Data Declaration
When a variable is related to the execution context, it should know where the data is stored and how to obtain it. This mechanism is called a variable object.
A variable object (VO) is an object related to the execution context. It stores:
- Variable (var VariableDeclaration );
- Function declaration (FD );
- And function parameters.
Declare in the context |
For example, it can be a variable object of a normal ECMAScript object:
VO = {};
As we said, VO is the attribute of the execution context:
activeExecutionContext = { VO: { // context data (var, FD, function arguments) }};
Indirect reference variables (through the VO attribute name) are limited to variable objects in the Global Context (where the global object itself is a variable object, which will be involved later ). For other scopes, it is impossible to directly reference a VO. It is completely an execution mechanism.
When we declare a variable or function, we do not create a new property for VO. This property has the name and value of our variable.
For example:
var a = 10;function test(x) { var b = 20;};test(30);
The corresponding variable object is:
// Variable object of the global contextVO(globalContext) = { a: 10, test:<reference to function>};// Variable object of the "test" function contextVO(test functionContext) = { x: 30, b: 20};
However, during the execution period (and in the Specification), the variable object is an abstract entity. Basically, in the specific execution context, VO has different names and different initial structures.
- 5 pages in total:
- Previous Page
- 1
- 2
- 3
- 4
- 5
- Next Page