it turns out

When JavaScript (books) had no closures

Update: As reddit user itsnotlupus pointed out, JavaScript may not have actually had closures at all (!) in early 1997, when the earliest of these editions came out. I will try to track down more information, and make appropriate corrections to this article.

JavaScript is a survivor among web technologies, 20 years old and as relevant as ever.

Over that time the way in which we use it has evolved greatly. What started as tiny scripts that changed images on mouseover has given way to huge web apps that make many offline applications obsolete.

This evolution is evident even among the unchanged features of the language. For example our treatment of closures has changed dramatically over the years, as can be seen in the way one book wrote about them.

"JavaScript: The Definitive Guide"

David Flanagan's "JavaScript: The Definitive Guide" is a rare example of an in-print JavaScript book that is almost as old as the language itself. It has been in print non-stop since 1996, getting significant updates every few years.

It was indispensable in the days before Google and StackOverflow, and it remains one of the most influential JavaScript books. Netscape's Rhino engine got its name from The Definitive Guide's cover animal, not the other way around.

'The Definitive Guide' over the years. Lined up in order are the editions from 1997, 2001, 2006, and 2011
"The Definitive Guide" over the years

Closures

Closure is a programming language construct that ensures that a function can always access the scope in which it was created. It is an integral part of JavaScript, necessary since the start. And it could never be changed without sacrificing backward compatibility.

This is an important point. Of course older editions of the book do not cover features introduced in EcmaScript 5, and newer ones do not devote an entire section to "Arrays in [Netscape] Navigator 2.0". But closures have been the same since day one. This makes them an excellent candidate for chronological comparison.

You would think that since closures never changed, neither did the book's treatment of them. But in fact there was great change.

This isn't because the language changed. It is because the way we use the language changed.

Closures in The Definitive Guide

With help from Toronto Public Library, I put together a timeline of closure coverage across various editions of Mr. Flanagan's book:

1997Not a word.
2002Two sentences.
2006A large section, shared with function scope. It contains extensive examples of closures.
2011A large section, dedicated to closures alone. Many other parts of the book demonstrate programming techniques that make use of closures, referring back to this section.

It is most striking to visually compare the index entries for closures across various editions:

1997 cover of 'The Definitive Guide' 1997 index of 'The Definitive Guide'. Term 'closure' doesn't exist.
2002 cover of 'The Definitive Guide' 2002 index of 'The Definitive Guide'. 'Closure' refers to one page.
2006 cover of 'The Definitive Guide' 2006 index of 'The Definitive Guide'. 'Closures' have a 6-page section and 2 more references.
2011 cover of 'The Definitive Guide' 2011 index of 'The Definitive Guide'. 'Closures' have a 6-page section and 7 more references.
Closures in the index of "The Definitive Guide" over the years

Why this is

While closures never changed, their relevance did. This is because the way we use JavaScript has changed.

If you just run simple onclick scripts, you never run into closures. To run into them, you need a function that creates and returns another function. This is a useful, but advanced programming technique. You are unlikely to need it until you are writing a complex web app. That's why questions about closures are popular in job interviews. They are a good proxy for "Have you programmed a large web app in JavaScript?"

The book's coverage of closures reflects the way our use of JavaScript has evolved:

  • In 1997 nobody programmed large JavaScript web apps.

  • In 2002 only a handful did. (I think Outlook Web Access for Internet Explorer 5, released in 1999 along with the necessary XMLHttpRequest technology, may have been the first true "Web 2.0" app.)

  • By 2006 early open source frameworks like Dojo and Prototype were popular, and jQuery was soon to follow. JavaScript programming had grown up.

  • By 2011 everyone knew jQuery, and modern JavaScript MVC frameworks were arising. Most JavaScript programming involved large web apps, which needed to be managed using techniques based on closures, like encapsulation and scoping.

I hope Mr. Flanagan is working on the next edition. Although I think his coverage of closures is already maxed out.

comments on Disqus