John Resig: Secrets of the JavaScript Ninja

February 11, 2013

My distinguished colleague Josh Heyer, better known as the infamous Shog9, suggested I treat myself to jQuery inventor John Resig's Secrets of the JavaScript Ninja. Josh was an early reviewer of the book, and he usually knows what he's talking about. So of course I bought the book.

I actually bought the dead-tree version from Amazon, and was pleasantly surprised that the first thing you see when opening the book is an instruction on how to download the ebook from the publisher for free. Thanks Manning, this is treating your customers well. And it makes Amazon's “let the publisher know that you would like to read this on the Kindle”, which appears at least on amazon.de, pretty pathetic – they already let you read it on the Kindle, and they do so without any of the well-beloved Amazon DRM.

Anyway, I digress.

try

It took me about one and a half days to get through the book, which is a testament to the readable style in which the book is written. Resig manages to present code and talk about it in a way that makes it easy to follow, without resorting to overly simplified and contrived examples. In many programming books, you often have to stop after each prose sentence and think “Wait, which line of the listing is he talking about now?”. The Ninja book in contrast somehow manages to present itself almost like the author is talking about code that's projected onto a screen, making it possible to follow both the code and the explanation at the same time.

The writing style is friendly, not dry, without being facetious. Most of the time, at least. And there are ninjas and sumarais, and even jedis!

It's hard to find a succinct quote that exemplifies a writing style, but here is a try. This is how Resig describe the problem with long-running operations in JavaScript's single-threaded event model, and how to solve this problem with setTimeout.

You may have been to a family reunion where a garrulous uncle won't stop talking and insists on telling the same stories over and and over again. If no one else gets a chance to break in and get a word in edgewise, the conversation's not going to be very pleasant for anyone (except for Uncle Bruce, of course). Likewise, code that hogs all the processing time results in an outcome that's less than desirable; producing an unresponsive user interface is never good. […]

These are occasions when timers can come to the rescue and become especially useful. As timers are capable of effectively suspending the execution of JavaScript until a later time, they can also break up the individual pieces of code into fragments that aren't long enough to cause the browser to hang. […]

What we need to do is shut Uncle Bruce up at regular intervals so that other people can get a chance to join the conversation.

This prose is accompanied by code samples that actually show how this breaking up works. Although the author then goes into great detail explaining the code, he does not fall into the trap of beating the dead horse that is Uncle Bruce. It's a good story to illustrate the problem, but in the end we're talking about code, and metaphors only go so far. And the author realizes this.

What's inside?

Let's get from style to content now. Who is this book for? Let's put it this way:

  • If you have you have just started playing with JavaScript, this book isn't for you. Yet.

  • If you're comfortable with JavaScript closures, understand the prototype chain and the JavaScript event model, then the first half of the book probably won't teach you a lot of new things. It's still a nice read and refresher, but don't expect many “Oh wow, I didn't know that was possible!” moments.

    If you're generally comfortable with JavaScript, but some of these advanced topics aren't quite clear to you, then by all means read this. In particular, this book does an excellent job at explaining what closures are, how to get comfortable with them, and how to juggle them around.

  • If you're the kind of person who knows their way around in the jQuery source code very well, the second half likely won't have many surprises for you either. But either way, the second half is an interesting description of various browser and cross-browser issues that are definitely worth having heard of.

    However it's more a collection of anecdotes than a list of recipes. Remember, this is not an introductory textbook. You won't be spoonfed solutions to your own everyday cross-browser issues; you will be told stories of solved issues, and all the book can offer is a look into the necessary mindset and a certain amount of inspiration on how to tackle such issues.

catch

There are a few gripes I have with the book, which I will explain. To spoil the ending: Despite these gripes, I still encourage you to give it a read.

Signs of age

You really see that much of this book was written years ago. This doesn't invalidate the book, but it means you should read it with a few grains of salt. On his blog, John Resig gives some insight on where this delay came from.

A lot has happened in the world of JavaScript and web devlopment in those years. Obviously the author is the first person to know and acknowledge that – but I can't lose the feeling that parts of the book were just patched where they showed age, and then rushed out the door.

To me, the most glaring example of these five years in the making is the following: In chapter 4, “Wielding functions”, the book introduces arguments.callee. Pretty much the first thing in that section is the following (very important) warning:

The callee property is on the chopping block for an upcoming version of JavaScript, and the ECMAScript 5 standard forbids its use in “strict” mode. It's OK to use this property in current browsers, but its use isn't future-proof, and we'd likely not want to use callee in new code. Nevertheless, we present it here as you may come across it in existing code.

It is a fair point to introduce callee for this reason. But to me it seems more like this section was written in 2008 – when arguments.callee was alive and kicking –, and then the warning was tucked on for the final release of the book. Case in point: In chapter 14, “Manipulating the DOM”, the book describes a problem in Internet Explorer with cloning of DOM elements, and gives an example from the jQuery code of how you can check for the presence of this problem. And this code snippet makes use of arguments.callee.

It's not vital to this code; the point of listing 14.8 is somewhere else – but I would expect a deprecated language feature (which the book called out as such previously, multiple times) to not be used in example code. After all, John Resig himself removed this use of arguments.callee from jQuery in early 2009.

You may find this to be nitpicking, but it unfortunately illustrates the long coming of this book. Much of the book is timeless, so it's not the biggest of issues. But when reading, keep in mind that not everything may be as up-to-date as you would expect from a book released in late 2012.

Dangers

In a book that strives to make you a ninja, I would have expected some more discussion about edge cases. You don't have to handle or even mention all of them no matter how unlikely. But some pointers to possible situations that aren't as easy as the simplest case would be nice.

For example (this is in section 6.3, “Writing class-like code”), when using a regex on a function's string representation to check whether the function uses a certain feature, one should mention that this approach is making an assumption: The function is using this feature by its original name, and this name actually appears in the function source, and not e.g. in a string variable coming in from outside the function.

In another example, when introducing function overloading based on arguments.length (in chapter 4), but later doing it by checking arguments for undefined (e.g. in chapter 12), it deserves a mention that these two are not equivalent. It may not make a difference in this particular case (or, for that matter, in those many places where this is done in jQuery), but from a general point of view, this is the kind of thing a ninja has to be aware of: Passing undefined as a function parameter and passing nothing at all are not the same thing.

On a final minor note: I am for the most part a proponent of the Crockfordian school of thought. Things like a comparison != null and decrement-operator-as-expression are not a good idea for code readability in my view. Thus I am not a huge fan of some elements of Resig's coding style. But of course this is personal preference. And I can't deny that this coding style gave us the most-used JavaScript library of all times, a library that probably did more good for world-wide developer productivity than Stack Overflow and caffeinated beverages combined. So who am I to argue.

finally

In conclusion, I do recommend this book for anyone who knows their JavaScript basics and wants do broaden their horizon. It is well-written, and it's great at teaching you about some advanced parts of the language, about real-world issues, and about pragmatic solutions.

Keep the caveats above in mind when reading the book, and never hesitate to disagree (that should be true of any book, though). But don't miss the chance to hear from a JavaScript guru who knows what he's talking about, and who – to stay with martial metaphors – has been in the trenches.

Just don't expect this book to make you a ninja. It may give you a lot of weapons, powerful and dangerous at once. But to really become a master, you need to practice using them. Out there, fighting, in the real world.




previous post: An unexcited look at browser sniffing

next post: Plain text considered harmful: A cross-domain exploit

blog comments powered by Disqus