Secrets of the JavaScript Ninja: Chapter 5 Review

Closing in on Closures

Closures are how you can maintain scope and context in your application. Without them, far too much ends up cluttering the global scope. With them, you can truly have modular code. There are several things in the language that mark the passage of a person’s skill to the next level: closures are a huge one. I would argue that until you get the hang of closures you will forever remain at a novice/beginner level of JavaScript skill.

This chapter covers closures and how they are used to control scope/context. It also covers their use as immediate functions (fire and forget). The primary example tracked through the chapter is currying, but there is also a good memoization example.

What I Learned in this Chapter

I’ve known about (and heavily used) closures for many years. It was good to get a professional refresher with a batch of simple examples, but I can’t honestly say I learned much in the particular chapter. Some of the examples used some interesting styles that I pondered (like returning an equality statement as a shortcut).