How to follow the slide deck
Preparation: Fire up your favourite text editor, a tab on the Chrome browser, an open the debugger console by pressing “Ctrl-Shift-J” or “Option-Shift-J” depending on the breed of your computer.
You’re expected to type the code presented in the slide deck into the text editor, copy-paste it into the debugger and run it. While I encourage you to literally type in code that you see for the first time, you may make the presented variations of the code by copy-paste (ex: adding “debugger;” statements).
On closures, dogs, frisbees and Sir Isaac Newton
A colleague of mine noted that it is amazing how people pick up the notion of functions in JS and do crazy things with it, all without an awareness of what exactly they’re doing. They write functions that take functions as arguments and return other functions without batting an eyelid. If one studies the code, it would seem that some kind of a deep understanding of the the concept of closures is at work, but this illusion is shattered the moment one steps into the code and starts probing why the code works the way it does.
Here is an analogy - actually one of my favourite philosophical questions that I enjoy leaving unanswered.
Consider a dog that’s adept at catching a frisbee thrown at it. This is an amazing feat if you look at all that needs to be done to achieve it. The dog has to track a flying coloured disc, anticipate where it is going based on its ingrained understanding of gravity, basic physics and aerodynamics, figure out how to coordinate its muscles with the right signals so that its mouth will make contact with the colured disc in such a way that the disc gets trapped between its teeth. All out of just experience. This is just wow!
Now consider Sir Isaac Newton, who thought through to the core of how all these things happen in the world and formulated the now famous “laws of motion” and “law of gravity”. Brilliant work and none would dispute that.
Who “understands” physics better - the frisbee catching dog, or Sir Newton?
I leave that question unanswered always because I can argue equally in favour of either side. For our purpose, I wish to highlight the advantages of each kind of understanding.
While the dog performs a superbly fluent function that Sir Newton cannot even dream of accomplishing to comparable fluency, the dog that doesn’t ponder the why and the mechanics of how it achieves what it does will never be able to make the connect between gravity on earth and the gravity between the stars and planets.
A mechanistic understanding of closures is a start and is comparable to grokking Newton’s laws of motion and gravity to predict what happens under various circumstances. A higher level of understanding will involve deriving beautiful overarching principles of physics such as conservation laws, the principle of least action and such. To have a glimpse of that, dive into the world of Haskell.