I was perusing JS: The Right Way recently and they mentioned closures under “The Good Parts” section. This reminded me of the difference between closure scope and the prototype chain: variable lookups are a scope lookup, while (failed) property lookups use the prototype chain. In other words, according to JS: The Right Way, “the function defined in the closure ‘remembers’ the environment in which it was created.” The environment, as Mozilla puts it, consists of three scopes: local scope and outer function scope that were in-scope at the time the closure was created, plus global scope.
I find closures to be an interesting topic because they allow the creation of variable privacy that only those functions returned out of the parent function can access. This may help mitigate bugs since it cleans up the global namespace. 
One way to implement a closure is by creating state. As a way to control how we use/change the state, we can keep it private by using closure, and only allow certain methods to interface with the state. These methods are returned from the parent function and invoked with event handlers, for example.
Below is a CodePen example using closure to control an array that is being implemented with undo/redo functionality.
For more on closure, I recommend Will Sentance:
Frontend Masters (subscription)
Shortened YouTube version

You may also like

Back to Top