JavaScript Patterns, A Book Review ~ Thu, 23 Dec 2010 16:42:20 +0000
Around mid-November I got an email from O'Reilly with a discount code on select e-books. One of them caught my interest, so I bought it and have been reading it over the last month. That book is JavaScript Patterns by Stoyan Stefanov. I am only midway through chapter 6, and I was going to wait until I completed the whole book before writing this, but it's a pretty slow day and I think I have read enough of this book to know that I recommend it.
JavaScript Patterns isn't a typical book of its sort. Instead of wasting time teaching you the language, it gets right to the point and starts talking about using that language, JavaScript, in creative and useful ways. That's not to say the book will be lost on someone who is not a JavaScript guru. I have learned the language the old fashioned way -- trial and error and reading other people's code. I don't consider myself a master of JavaScript by a long shot, but I have found this book easy to follow and understand. Which I suppose is why I like this book as much as I do. I have seen most of the "patterns" used before and this book explains them in a short, easy to understand, manner. There are no wasted pages in this book.
If you look through the table of contents on the O'Reilly product page you might get the impression that this book is simple. Sections like "Minimizing Globals," "Writing Comments," and "Object Literal" make it seem like there is nothing of consequence in this book. That hasn't been the case for me; I have learned something useful from just about every section (okay, "Writing Comments" is a bit obvious). For example, one of my highlights comes from the "Minimizing Globals" section:
Hoisting: A Problem with Scattered vars JavaScript enables you to have multiple var statements anywhere in a function, and they all act as if the variables were declared at the top of the function.
It goes on to discuss a pattern that I have started to use in my own code, and can be seen in my JavaScript cookie manager object -- declaring, and in some cases defining, the variables for a function at the top of the function in one var statement. If the compiler is going to do that behind the scenes, then why not do it yourself? The book goes on to describe how this language feature affects the way functions are defined and the gotchas that implies.
Speaking of my cookie manager object, the var declaration pattern isn't the only thing present in that code from this book. I was aware of, and had been using, the self-executing function (immediate function) for some time before reading JavaScript Patterns. What had never occurred to me was the concept of a self-executing object literal. My cookie manager object is just that -- an object literal that executes as soon as it is defined by the JavaScript interpreter. I think it has fewer uses that the immediate function, but in certain cases, like the cookie manager, I think it makes much more sense.
JavaScript Patterns also references popular JavaScript libraries when discussing the usage of various patterns in the real world. The author is a Yahoo! employee and contributor to the YUI library, but he also talks about the jQuery library. It's nice that he doesn't limit his discussion to only those patterns that the YUI team have settled upon.
In short, I recommend this book to anyone who is familiar with JavaScript and wants to have a deeper understanding of code used all over the web (YUI and jQuery in particular). I also recommend this to the JavaScript developer that just wants to improve their own code. It's a thorough, but terse, discussion of actual JavaScript code. This isn't a book about what the JavaScript reserved words are and how they work; this is a book about writing good code.