跪拜 Guibai
← All articles
JavaScript

Scope, Closures, and Objects: How JavaScript Keeps Variables Alive After Functions Return

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

Misreading these rules produces real bugs: shared loop variables, unexpected value binding, and memory bloat from retained closures. This breakdown gives developers the mental model to predict variable lifetime and lookup order in any JavaScript codebase.

Summary

The article builds a mental model for three connected JavaScript mechanics. Scope defines where variables are visible, and the rules are one-way: inner functions can see outer variables, but not vice versa. Lexical scope matters more than call location, which is why a function defined globally still reads the global variable even when invoked inside another function. Closures then explain what happens when a function outlives its outer function: referenced variables are retained in a persistent set, enabling private state, counters, and the classic `var` in a loop problem that `let` solves by creating per-iteration bindings. Finally, objects and the prototype chain determine how variables and methods are stored and shared, along with the wrapper class behavior that prevents primitive values from holding properties. The article ties these together with the opening puzzle: `getAge()` returns `18` because lexical scope grants access, closures keep the variable alive, and the structure that carries it is ultimately object-like.

Takeaways
— Variable lookup follows a scope chain from inner to outer until the global scope; missing identifiers throw an error.
— Lexical scope binds variables based on where a function is defined, not where it is called.
— eval() can bypass lexical scope but harms performance and security; it should be avoided.
— let and const create a temporal dead zone before declaration, causing errors instead of undefined.
— Closures preserve referenced outer variables after the outer function returns, enabling private state and counters.
— var is function-scoped, so loop variables are shared; passing the current value as a parameter or using let solves this.
— An instance's __proto__ equals its constructor's prototype, so methods defined on prototype are shared across instances.
— Primitives cannot retain added properties because the wrapper class mechanism unboxes immediately.
Conclusions

The article explicitly separates the call stack from the scope chain, correcting a common conflation that muddles how variable lookup and function return are handled.

Treating a closure as the retained variable set, rather than just the function, makes the memory implications and the mechanism behind private state clearer.

Concepts & terms
Closure
A set of variables retained by an inner function after the outer function returns, allowing the inner function to access them.
Lexical scope
The rule that variable references bind to the location where the function is defined, not where it is called.
Prototype chain
The chain of __proto__ references that JavaScript uses to find properties not present on the object itself, ending at Object.prototype or null.
Wrapper class
The temporary object JavaScript creates around a primitive value (e.g., new String(s)) when accessing properties, which is discarded immediately so properties do not persist.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗