JavaScript Patterns

**** Mar102013

JavaScript Patterns by Stoyan Stefanov is a straightforward, densely packed book of best practices, things to avoid, and most centrally, patterns for writing maintainable, extensible, and performant JavaScript.

As stated in the preface, “This book is not a beginner’s book”. It is, however, an excellent follow-up to the previous JavaScript book reviewed here, JavaScript: The Good Parts, by Douglas Crockford. Many of the concepts briefly presented there are expanded on or further explained in this book.

Basics and Beyond

Chapter 2, Essentials, re-enforces some coding best practices like well-considered comments and documentation, avoiding global variables, and minifying your code for production. Chapters 3 and 4 cover some more complex topics like constructors, the distinction between a function declaration and a function expression, call back functions, and immediate functions (aka self-executing, aka immediately-invoked functions). Even if you are not an experienced JavaScript programmer, some of these patterns will look familiar if you’ve used a library like jQuery. The jQuery source code itself, for example, is one big immediately-invoked function:

(function(window, undefined) {
	///all of jQuery's goodness!
})(window);

Complex Design Patterns

Chapters 5, 6, and 7 delve into yet more complex patterns, including ways of encapsulating code, inheritance concepts in JavaScript’s classless environment, and finally application design patterns. These chapters may be slightly more suited to programmers coming from another language. For example Mr. Stefanov explains how to implement a singleton in JavaScript:

“The idea of the singleton pattern is to have only one instance of a specific class.”

However, as someone whose only language is JavaScript, which doesn’t have classes in the first place, I’d rather know why and when a singleton is useful than how its implementation differs from other languages.

Memoization? Maybe Someday

For me JavaScript Patterns is somewhat of an aspirational book. I hope to, some day, have the need (and the cleverness to recognize it) to implement the “Memoization Pattern”, whereby a function caches the values it computes, so when it is next asked for the same value, it can skip the expensive computation (p. 76). I can see a more immediate use, though, for many of the patterns presented. The module pattern for example, is a very common and useful way to encapsulate and organize JavaScript code (p. 93), and lazy loading scripts is quite straightforward and useful these days when even a simple webpage may involve several third-party scripts (p. 203).

Where Be the Templates and Promises?

There were a few things I was disappointed not to see in this book. In the last chapter “DOM and Browser Patterns”, the author discusses some ways you can minimize DOM access and manipulation, which can be slow and create performance problems. I expected to see some mention of JavaScript templates here, and related best practices, as they are commonly used in web apps to render repeated bits of html.

Also, though the author briefly addresses AJAX (very briefly — just over one page), I expected more here, especially using the promise pattern, which can be a better solution than having multiple or complex callback functions.

Another slightly anachronistic quirk of the book is that, while totally “library-agnostic”, when the author does mention a library, it is YUI, which ranks pretty low on the market-share list at this point.

Conclusion

I chalk all of these oddities up to the fact that this book was written in 2010, and web technologies have developed at a dizzying pace since then. Given the valuable content in the rest of the book, an updated second edition would be welcome, but even as is it is worth a read.

Respond to this review

Author(s)Stoyan Stefanov
PublisherO'Reilly
Pages236
Published2010
ISBN-100596806752
Category
Tags
Rating****

No comments yet…Be the first!

Comments are closed for this article.