Understanding JavaScript Variable Objects: From Creation to Execution
This article explains how JavaScript creates and uses the Variable Object during the creation and execution phases of an execution context, covering arguments, function and variable declarations, hoisting, scope chain, and the relationship with the global window object.
In JavaScript we inevitably declare variables and functions, but how does the engine locate them? Understanding the execution context is essential.
Creation Phase
During this phase the execution context creates a Variable Object, builds the scope chain, and determines the value of
this.
Code Execution Phase
After creation, the code runs, variable assignments and function references are resolved.
Variable Object (Variable Object)
The Variable Object is built through three steps:
Establish the
argumentsobject, mapping parameters to properties.
Process function declarations: each function name becomes a property whose value is a reference to the function. If a property already exists, it is overwritten.
Process variable declarations: each
varcreates a property with the value
undefined. Existing properties are left unchanged to avoid overwriting functions.
These rules make variable hoisting easy to explain in interviews. Function declarations have slightly higher priority than
vardeclarations.
Example
Consider the function
test()called in the global scope. Its execution context is created, then the Variable Object becomes an Activation Object during the execution phase, allowing its properties to be accessed.
Before the execution phase, properties in the Variable Object are inaccessible; after entering the execution phase, the Variable Object turns into an Activation Object, and its properties become reachable.
Another example reinforces the concept.
Understanding how the Variable Object changes from creation to execution solidifies mastery of JavaScript’s execution context.
Global Context Variable Object
In browsers the global object is
window. The global execution context’s Variable Object is the
windowobject, and
thisalso points to
window.
The global context lives as long as the program runs; closing the browser ends it. All other contexts can directly access the global context’s properties.
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.