Book Reviews

JavaScript Patterns by Stoyan Stefanov

ISBN: 978-0596806750
Publisher: O'Reilly Media
Pages: 236

JavaScript has grown to become one of my favorite languages. Behind the superficial ugliness, there's an elegant core. I owe much of what I learned on JavaScript to Douglas Crockford's excellent JavaScript: The Good Parts. That book defines a minimalistic and powerful subset of the language that is immediately useful. Crockford's work also assumes a certain level of knowledge that I didn't possess at the time of reading. Some coding later, I begin to understand the conceptual foundation. And JavaScript Patterns is an excellent second book on the subject.

This book has a certain overlap with Crockford's. JavaScript Patterns can be seen as a gentle and expanded coverage of the material in Crockford's book. JavaScript Patterns start of with a brief introduction to coding practices and basic JavaScript before diving into functions. And this is where the interesting part starts. JavaScript is a functional language with highly dynamic characteristics. This allows for expressive code and Stoyan Stefanov skilfully captures and explains the common idioms. We get coding patterns like self-defining functions and init-time branching before moving on to more complex patterns.

And there's a thing about patterns; every pattern exists in a context. A highly relevant context for a pattern is the programing language. Patterns that are vital to good design in one language may loose their meaning in another context or even turn into anti-patterns. This becomes evident as the book moves on to implement GoF patterns. Most patterns in GoF are oriented towards statically typed message-passing OO languages. In a language like JavaScript they loose their meaning. Although questionable from a design perspective, I still found the GoF implementations useful. Not at least as examples on well-written JavaScript code. The next chapter on code re-use is another highlight. Here we get an extensive coverage of the different mechanisms for code re-use, including both classical inheritance as well as modern prototypal inheritance patterns.

One thing that attracted me to JavaScript was how small the language is. Constructs that are normally part of a core programming language are missing in JavaScript. Instead, features like namespaces and modules are implemented using well-known patterns. Stoyan covers all of these in an accessible and pedagogical way. There's plenty of code samples which complements the theoretical discussions. The text is well-written, the code samples pedagogical and straightforward. As my second real book on JavaScript, I couldn't have made a better choice.

Reviewed May 2012