Inside JavaScript's Precompilation: The Hidden Step That Decides Variable Values
JavaScript Precompilation Mechanism and Execution Context Construction Analysis
Abstract: This paper analyzes how JavaScript constructs execution contexts in global and function scopes by registering declarations before code execution. Global precompilation creates the global object, registers variable declarations, and writes function declarations to complete environment initialization. Function precompilation adds parameter declaration and argument binding, then processes function declarations within the function body. Using assignment sequences of same-named identifiers, we explain how the order of declaration registration affects runtime values, and discuss the call stack's role in recording call order, arguments, and return values.
Keywords: JavaScript; precompilation; execution context; activation object; call stack
1 Introduction
Before executing statements one by one, the JavaScript engine completes a declaration registration for the current scope. This process is called precompilation, and its product is the execution context object. Precompilation does not change the order of statement execution, but it alters the initial state of identifiers when code begins to run: variables and parameters are filled with undefined, while function declarations point to the function body. This mechanism helps explain why same-named identifiers can have different values during runtime.
2 Timing and Scope of Precompilation
Precompilation occurs before code execution. By scope, it can be divided into global precompilation and function precompilation. The former creates the global execution context object (global object, GO) when the script enters the execution phase; the latter creates the corresponding execution context object (activation object, AO) each time a function is called. The registration rules for both types are similar, with the difference that function precompilation must handle the correspondence between parameters and arguments, adding an extra binding step.
3 Construction of the Global Execution Context
3.1 Construction Steps
Global precompilation performs three operations in order: create the global execution context object GO; search for variable declarations in the current scope, set the variable names as properties of GO and assign undefined; search for function declarations, set the function names as properties of GO and assign the function body. Since function declarations are written after variable placeholders, when both use the same name, the property ultimately points to the function body, and the undefined left by the variable declaration is overwritten.
3.2 Separation of Declaration and Assignment
In Code 1, the script first assigns a value to global, then declares function fn, and at the end declares global with var.
global = 100
function fn() {
console.log(global);
global = 200;
console.log(global);
var global = 300;
}
fn();
var global;
In the GO created before execution, global is initialized to undefined due to the var declaration, and fn points to the function body. After the execution phase begins, the first assignment statement changes global to 100. Declaration and assignment belong to two separate phases; the properties in GO are determined jointly by precompilation registration and sequential execution.
4 Construction of the Function Execution Context
4.1 Formation of the Activation Object
A function call triggers function precompilation, whose steps are: create the execution context object AO; search for parameter and variable declarations, set the names as properties of AO and assign undefined; establish the correspondence between parameters and arguments; search for function declarations in the function body, set the function names as properties of AO and assign the function body.
4.2 Parameter Binding and Declaration Overriding
In Code 2, function foo takes two parameters, but only one argument is passed when called, and the function body contains both variable assignment and function declaration.
function foo(a, b) {
console.log(b);
c = 0;
var c;
a = 3;
b = 2;
console.log(b);
function b() {}
console.log(b);
}
foo(1);
The evolution of AO is: a, b, and c are first registered as undefined; parameter a is bound to argument 1, so its value becomes 1; function declaration b is written into AO during precompilation, with its value being the function body. During execution, the assignment b = 2 changes that property to 2, so the last two reads both get 2. After the assignment statement executes, c changes from undefined to 0.
4.3 Scope Differences for Same-Named Identifiers
When a variable declared inside a function has the same name as an external global variable, reads and writes inside the function affect the property in AO. In Code 1, the global inside fn is such a case: the property in AO changes from undefined to 200 and then to 300, while the global in GO remains 100. The same-named properties in the two context objects are independent of each other.
5 Support of the Call Stack for Execution
During a function call, the V8 engine uses the call stack to maintain call order, call arguments, and return values. Precompilation determines the initial state of AO at the start of each call, and the call stack records the nesting relationships and data transfer between calls. Together, they cover the context state and call relationships during the call.
6 Conclusion
The essence of precompilation is to complete the registration of identifiers in the scope before execution. Global scope creates GO, function scope creates AO; variables and parameters are initialized to undefined, function declarations write the function body, and parameter-argument binding then completes. At runtime, assignments override existing properties in statement order, so same-named identifiers present different values at different stages. The call stack, on top of this, maintains call order, arguments, and return values. By following these steps and their order, one can consistently deduce such execution results.