⚠️ Warning: this is an old article and may include information that’s out of date. ⚠️
Intro
There have been many attempts to answer this question in a succinct way, but I haven’t really been satisfied by them. Here’s my attempt at a (relatively) concise explanation.
This is adapted from a reply I posted on reddit which people seemed to have found useful.
Broad definition
Normally when people talk about closures in JavaScript, they’re talking about methods and properties that outlive the scope of their original function (more on that in a second), but actually the definition is a bit broader. A closure is how a function “closes over” (Crockford) its variables and creates a different scope for them out of the way of the global [window] scope. So an understanding of closures will require some understanding of scope, which is definitely a common stumbling block in first learning JavaScript.
Essentially, closures make this possible:
|
|
Variables that survive after function execution
But when people get excited about closures, they’re not really talking about the example above, but about the ability for functional variables to outlive their original functional scope:
|
|
But note that the original method name (myPublicVar) isn’t available - but the value IS available from what we exposed to the global scope through the variable ‘b'.
Closures and the Module Pattern
How is this useful? This turns out to be the foundation for the Module Pattern in JavaScript - the building block of modern JavaScript applications. They’re in essence a glorified version of the block of code above, with the purpose of hiding the function’s private variables (this comes by default when declaring variables with the ‘var’ keyword in functions), and exposing public variables. And note that because functions are first class in JavaScript, those variables can even be references to functions themselves!
So in essence this is the module pattern, with both private and public properties and methods:
|
|
By the time we reach the last line of the above script, the function has stopped executing, but some original pieces of the function still live on! Because some references to functional variables were returned by the function, we can now access these through the “namespace” myAPI:
|
|
But those variables that were not exposed are of course not accessible:
|
|
Access to unexposed variables
Exposed public methods still have the context of the original function, meaning that they have access to all the function’s variables, public and private:
|
|
There’s definitely more to be said here, but hopefully this helps!
Comments