Book review: Pro JavaScript Design Patterns

by Ross Harmes and Dustin Diaz | Published by Apress
Pro JavaScript Design Patterns

When I first got my copy of Pro JavaScript Design Patterns I couldn't help but notice that Apress had altered their trademark black and yellow cover design to black and orange, undoubtably because of their similarity at first glance to the For Dummies books. After all, these are "books for professionals by professionals" - it wouldn't do at all to have one get into the hands of a dummy! (:-P) It's a little ironic, then, that my first point in praise of this book should be that it made some fairly technical concepts eminently accessible to dummies - I'd previously had a hard time getting a handle on things like closures and the prototypal chain but this Pro book finally did the trick. As it turns out, the book is extremely well targeted, having two particular types of reader in mind in its presentation of the material. The first type is the web developer who has used smatterings of JavaScript here and there but wants to understand how to write more elegant and efficient code in their js-based web applications; the second is the seasoned OO programmer who is curious about how Object Oriented concepts are implemented in JavaScript. Not having much of a programming background myself, I fit squarely into the former category.

When you haven't had formal training in any programming language but have just learned one or two by hacking around, you can end up being able to use a language to achieve your goals without having any appreciation for the fundamental concepts that underlie it and distinguish it from others. I suppose this is somewhat analogous to spoken languages in that you can be a native speaker of, say, German, without having any understanding of German grammar, if you've never tried to learn any other language. Anyway, I realised I was in this situation with regard to programming languages when I read about scope, nested functions and closures in chapter 3. I had been using JavaScript for some time, albeit mainly by leveraging the awesome power that is the jQuery framework, without ever realising the significance of how its variable scope works. Look at this unassuming little piece of code:

function foo() {
  var bar = 5;
 
  function baz() {
    bar += 2;
    return bar;
  }

  return baz;
}

The casual observer would never suspect that it holds the key to something as important as encapsulation, but the fact that the inner function has access to the variable bar is exactly what encapsulation is built on in JavaScript. The authors do a good job not just of explaining what encapsulation is, for starters, but of getting the novice OO programmer to really see and appreciate JavaScript's particular way of implementing it before going into detail about exactly how something like the above can be used to create classes with private instance variables and public methods. The book is divided into two parts. The first part covers these kinds of basic principles and discusses the peculiarities of the JavaScript language that enable it to emulate things like interfaces, inheritance, and singleton objects, which are not built into it like they are in languages such as Java and C++. Inheritance, for example, is achieved with the extends keyword in Java but there's no such keyword in JavaScript. Well, using the prototypal chain and creating a fairly simple extends() function you have all the inheritance functionality you'll ever need. The basic OO concepts are explained briefly enough not to bore those already familiar with them (I imagine!) but also clearly enough for us newbies to get a pretty good grasp before going on to look at how they can be implemented.

The second part of the book looks at particular design patterns that have become common in Object Oriented programming and shows how they can be used in JavaScript. These include the factory pattern, the composite pattern, the adapter pattern, the decorator, the observer and several others. For each of these it explains the structure of the pattern, gives an example of how it can be implemented, explains in which situations its use might be called for and discusses the benefits and drawbacks of its use. This part of the book assumes you have read Part One but each individual pattern covered can be dipped into in any order so that you can pick just those patterns that you think will be of interest to you without having to read the ones that come before.

My appreciation of JavaScript has changed since reading this book from a "woah, look at all the flashy crap I can do!" (to use a webchick phrase :-P) type of attitude to one of genuine, albeit limited, understanding of the subtleties of the language itself. Now it's more like, "woah, I can pass a function as a parameter to a function or have a function as the return value of another function!". I'm a long way from having fully grasped everything covered in the book - too much of it was so new to me - but it did enable me to completely restructure the js code in my Quick Tabs Drupal module to use Object Oriented principles, something I would never even have attempted (or considered, for that matter) beforehand.

As for the pace of the book, I'd say seasoned OO programmers will probably fly through it pretty fast as the concepts will already be so familiar. I did find parts of it pretty challenging to get my head around but the information is presented so clearly it usually just meant I had to pause on particular explanations and allow the cogs in my brain to grind away until I grasped the concept being explained.

The book has got me not just really into JavaScript in a way that I never was before, but also really into learning more about OO programming - and as a Drupal developer, seeing as there seems to be an increasing use of such techniques (e.g. the Views 2 code), this is probably a pretty good time to start learning this stuff.

Back in the early days Design

Back in the early days Design Patterns examples mostly used C++ as language. This day Java is the typical choice, with an increasing amount of C#. Usually if you are familiar with any of those languages, porting the sample code to a different platform isn't that hard. Unfortunately JavaScript is quite a different beast, first of all is not class based, it's loosely typed, it doesn't support interfaces etc. JavaScript has a whole bunch of peculiarities that make it really hard to translate those Java/C# samples. That's why this book is really welcomed. The authors stick with mainstream, well-know patterns, nothing new, but they "translate" those patterns in JavaScript, offering working samples. I tend to disagree with them whenever they pretend to add interface-like functionality to JavaScript; yet, I think they made an excellent job, a book that can be really valuable if you do non-trivial work with JavaScript

affordable degree | online phd degrees

This book is a "JavaScript

This book is a "JavaScript for Programmers" book. It's very detailed and advanced. I wouldn't classify it as a "JavaScript for Web Weenies" or a "JavaScript for Stupid UI Tricks" book at all. If you don't know OOP concepts in another language already, this book really might be too much for you and could frustrate you. This book is good if you are a server-side programmer and you want to know how to push JavaScript about as far as it can go (at this point). If you are a web programmer/designer with a light understanding of OOP concepts and you want to "go deep", this book could be ok, but I would try to learn OOP from another language first because this book would read better with that background knowledge. Also, I am recommending that you already know OOP from another language because you can really shoot yourself in the foot with JavaScript because it's *so* flexible and the authors prove this well! I think it's probably a good idea to know when you're going off in the weeds and JavaScript really doesn't provide many boundaries where other languages have stricter controls on what you can do.

college degree | accredited degree | online diploma