Master JavaScript Closures: Unlock Private and Public Scopes

This article explains JavaScript closures, covering variable scope, how closures bridge inner and outer functions, and demonstrates practical uses such as creating private/public scopes and correctly attaching DOM event handlers in loops, with clear code examples and visual illustrations.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Master JavaScript Closures: Unlock Private and Public Scopes

JavaScript closures provide great convenience for developers; they feel natural enough that many use them without fully understanding the concept.

To grasp closures you first need to know JavaScript variable scope, which consists of global and local variables. A function can access variables defined outside of it, but code outside the function cannot access the function’s internal variables.

Reading a local variable from outside is achieved by defining a nested function inside the outer function; the inner function can access the outer variable, and the outer function returns this inner function, allowing external code to reach the local variable.

What is a closure? A closure is a bridge that connects the inner and outer scopes of a function, allowing external code to access variables defined inside the function. In essence, a closure is “a function defined inside another function.” The inner function retains a copy of the variables from its parent scope, and these variables persist in memory even after the parent scope finishes execution.

Closure application examples

(1) Implementing public and private scope control

Because a closure can expose internal variables, it can be used to create public/private-like scopes.

Public private scope example
Public private scope example

(2) Adding DOM events inside a loop

When attaching click events to multiple input fields, a common mistake is that the loop variable’s final value is captured, causing every handler to reference the same (last) item.

Incorrect event handler example
Incorrect event handler example

The correct approach uses a closure: each iteration creates a new inner function that captures the current loop variable, preserving its value for the event handler.

Correct closure-based event handler
Correct closure-based event handler

This works because the closure saves the parent variable in its own scope, ensuring each click handler accesses the appropriate data.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptclosuresDOM Events
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.