“Pro JavaScript Design Patterns” Book Review

Pro JavaScript Design Patterns

For a while I’ve been meaning to read a book on design patterns, which are ways to design and structure solutions to common problems in order to create efficient and optimal results. I missed a chance to take a course on them back in college when I took Cryptology instead, and have since been meaning to sit down and read up on what’s been developed and what can be useful to me. So it was to my happy surprise when I stumbled across Pro JavaScript Design Patterns in the book store a while back.

The book is broken into two parts. The first part explains how object oriented programming concepts work in JavaScript and how to implement the ones that aren’t built into the language, such as interfaces and private members. The second part of the book focuses on a common set of design patterns and how they can be implemented in JavaScript. It covers the following patterns, giving a chapter on each one: Singleton, Factory, Bridge, Composite, Facade, Adapter, Decorator, Flyweight, Proxy, Observer, Command and Chain of Responsibility.

Before I go any further, while I’m going to write a mostly positive review for this book, I should say that I initially tried to read it around 18 months ago, but stopped after I finished the first section. I re-read it from start to finish about 2 months ago. I stopped the first time I tried to read it because the book introduces a lot of ideas and I was a little confused on some of the concepts it touched on. Specifically, I was confused with how constructor functions worked and how a function’s prototype property worked. I got distracted trying to understand this and picked up another book and ended up not coming back to this book (it’s hard for me to get back into a book once I’ve set it down for a while). As for constructor functions and the prototype property, a good explanation of them can be found here.

I had better luck on my second read through and found the second half of the book to be an easy read. Though each chapter focuses on a different design pattern, some of them use patterns introduced in earlier chapters, so it’s actually a good idea to read the chapters in order. Each of these chapters starts by explaining the basic idea behind the pattern and then goes into a couple of real world examples of how the pattern is used. The chapters close by discussing the benefits and drawbacks of the pattern they introduced and some advice is given to help you identify situations where using the pattern would be a good idea.

Overall, I found this be to be extremely interesting. I could possibly have learned about the different design patterns by reading online tutorials, but being able to look at solutions in JavaScript was very helpful. Also, after reading certain chapters, I felt like going back and rewriting some of my old apps (though I’ll contain myself). Knowing about this collection of design patterns is a huge plus for me and for that alone I’m glad I read this book. I wouldn’t recommend this book to someone who doesn’t have a good understanding of prototypal inheritance, but if you feel like you do, this book is worth checking out. If you don’t, I would recommend looking up some design pattern tutorials online and trying to learn about the common patterns, you’ll be glad you did.

On a side note, I was also amazed at how many of these patterns I’d seen before. It certainly gave me a better appreciation for some of the code bases I’ve seen in the past. I also found that I used some of the patterns already unknowingly, but that I didn’t have a name for the technique I was using.

If I could go back in time I would still probably take that Cryptology class, but I would also try and read up on Design Patterns too. If you have a few extra minutes, it’s worth reading up on them.

3 thoughts on ““Pro JavaScript Design Patterns” Book Review”

  1. Hi, I spotted your link to my blog post on JavaScript constructors. Thanks for that 🙂

    I just wanted to note (and I haven’t read the book you’re reviewing) that the design patterns mentioned seem to come from the original design patterns book (see http://en.wikipedia.org/wiki/Design_Patterns ) which I have read. That original book is heavily influenced by the C++ language and its constraints as a “static” purely class-based OO language (which are even worse than Java’s constraints).

    IMO most of the patterns can be reduced to a few lines of code maximum in a more flexible and functional language like JavaScript, and some of the problems the patterns try to fix seem to go away completely when you’re not bound to using classes and/or can use anonymous functions.

    What I wanted to ask is: is this book basing itself on the class-based object paradigm or are they showing the problems in class-based OO and giving solutions appropriate to JavaScript?

    Cheers,
    Joost.

  2. No need to thank me, you created a useful reference :).

    I had thought about getting the original design pattern book, but I wasn’t sure if it was too C++ oriented. I may still pick it up at some point though.

    Most of the patterns discussed in the JS Pattern book use the Module Pattern for creating objects that can contain private variables. The book doesn’t really call it that (I found that article when I was trying to figure out what to call it), but it spends time in its first section discussing how that setup works. This setup basically allows you mimic classical inheritance.

    There was also a section in the first part of the book that compared and contrasted classical inheritance (where the module pattern was used) to prototypal inheritance (where the code uses a clone function to create new objects), however, flipping again through the book’s pattern chapters, all of the examples seem to use the Module Pattern.

Comments are closed.