跪拜 Guibai
← All articles
JavaScript

JavaScript Closures: How the Scope Chain and return Keep Variables Alive

By 凌晨三点说晚安 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Misunderstanding scope chain and closures leads to bugs and memory leaks in real applications; knowing when closures form and how to release them keeps long-running JavaScript healthy.

Summary

JavaScript's closure mechanism is rooted in the execution context and its outer pointer. When a function runs, V8 creates an execution context with an outer reference to the enclosing context. Variable lookup follows this pointer chain until the global context, which is the scope chain. This chain is fixed by where code is written, not where it's called. A nested function example shows how inner functions find variables step by step.

Closures form when a function returns an inner function that references its variables. Normally, execution contexts are destroyed after the function completes, but if the inner function is returned and later invoked outside, the variables it references persist. A closure is an inner function plus the outer variables it captures. Three conditions must hold: nested function referencing outer variables, exposure of that inner function to an outer scope, and execution outside.

Memory leaks are a real risk: closures keep captured variables alive until the reference is released. Setting the closure reference to null allows garbage collection. Common questions about nesting, scope chain and closures are addressed: mere nesting does not create a closure, and the scope chain is the lookup mechanism while closures preserve the variables along that chain.

Takeaways
— Scope chain is determined by lexical position, not call site.
— Variable lookup follows the outer pointer until the global context; in strict mode, an unresolved variable throws a ReferenceError.
— Closures form only when an inner function is exposed to an outer scope and called there, while referencing outer variables.
— The three conditions for a closure: nested function referencing outer variables, exposure via return or global assignment, and execution outside.
— Setting the closure reference to null (e.g., addFn = null) allows garbage collection to reclaim captured variables.
— Closures enable private variables, as the counter example shows: count is only modifiable through the returned add function.
Conclusions

Closure retention is tied directly to the outer pointer in the execution context, giving a concrete mental model instead of an abstract definition.

Only the variables referenced by the inner function are kept, not the entire outer execution context.

Mere function nesting does not create a closure; exposure to an outer scope and external execution are required.

Nulling the closure reference is a simple but crucial memory management practice often overlooked in production code.

Concepts & terms
Scope chain
A chain of execution contexts connected by outer pointers, used for variable lookup. It is determined by lexical position, not call site.
Closure
An inner function combined with the outer variables it captures, which persist after the outer function has finished executing.
Lexical scope
Scope determined by the placement of functions in the source code, independent of how or where they are called.
Outer pointer
A reference in each execution context that points to the enclosing context, forming the scope chain for variable lookups.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗