跪拜 Guibai
← All articles
JavaScript

Inside JavaScript's Precompilation: The Hidden Step That Decides Variable Values

By 彩虹暴龙兽 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

This breakdown turns JavaScript's hoisting quirk into a predictable sequence of declaration registration and assignment. For developers debugging why a local variable shadows a global one or why a function declaration overrides a variable declaration, the exact order of precompilation steps removes the guesswork.

Summary

Before a single statement runs, the JavaScript engine precompiles the current scope. This phase registers variable declarations as undefined, binds function declarations to their bodies, and (in function scopes) links parameters to arguments. The result is the execution context: the global object (GO) for the script's top level, and an activation object (AO) for each function call.

The registration order is what separates predictable behavior from confusion. In a function, the engine first sets parameters and variables to undefined, then binds parameters to arguments, and finally writes function declarations. When a variable and a function share a name, the function declaration wins because it is written last. At runtime, assignments overwrite these initial values in statement order, so the same identifier can hold different values at different points in execution.

The call stack complements this by recording call order, arguments, and return values across nested calls. Together, precompilation and the call stack determine the observable behavior of identifier lookups and value changes during execution.

Takeaways
— Precompilation happens before code execution, building the execution context for the current scope.
— Global precompilation creates the global object, registers variable declarations as undefined, then writes function declarations.
— Function precompilation adds parameter and variable registration, binds parameters to arguments, then handles function declarations.
— When a variable and function share a name, the function declaration overrides the undefined placeholder.
— At runtime, assignments update AO/GO properties in statement order.
— A local variable with the same name as a global one shadows the global inside the function.
— The call stack records call order, arguments, and return values.
Conclusions

The sequence inside precompilation is the root cause of hoisting: variable declarations become undefined, then function declarations overwrite them, so a same-named function always wins.

The independence of AO and GO properties explains why local shadowing is a matter of context construction, not lexical scope rules.

The separation of declaration and assignment phases means that a variable's value during execution is a combination of precompiled defaults and statement order, which is why the same identifier can be undefined, a function, or a number at different times.

Concepts & terms
Precompilation
The phase before code execution where the engine registers variable and function declarations in the current scope, creating the execution context.
Global object (GO)
The execution context object for the global scope, built during global precompilation to hold declared identifiers.
Activation object (AO)
The execution context object created for each function call, holding parameters, variables, and function declarations.
Hoisting
The JavaScript behavior where variable and function declarations are moved to the top of their scope during precompilation, resulting in undefined for variables and the function body for functions.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗