Secrets of the JavaScript Ninja

Book cover image

Let me put it as mildly as possible: I don’t like Javascript. I think it’s an ugly language with all the wrong choices, and too few facilities to do decent development. I find it a pity that we’re stuck with it. Despite being a web developer, I have been concentrating on the backend, and neglected Javascript until now. I picked this book to remedy this neglect, and to see how the features of the libraries I used everyday were implemented. Both expectations got satisfied, the second one in a manner other than I expected.

Secrets of the Javascript Ninja does an extremely good job of teaching the fundamentals of Javascript the language. It omits the stuff you would just google, like how to split a string or get a random number, instead focusing on the fundamental aspects that are different from other imperative languages. The role of functions and closures are explained in three detailed chapters. These chapters discuss the many intricate details of how functions are created and used, and the many roles they have to take on since Javascript has functions and not much else. There are also some very interesting coding examples that could be useful in other languages too, such as a very simple way to overload a class method in five lines of code.

After the chapters on functions, the object-oriented facilities of Javascript are discussed. Thanks to this chapter, I at last understand the role of prototypes in Javascript. There is a pattern in the rest of the book that starts in the object orientation chapter, though: Since Javascript does not have most of the facilities that many other languages (and environments, for that matter) have (such as proper inheritance), the authors dive into implementing these in Javascript. These attempts lead to very interesting code listings which are sometimes really difficult to parse, and sometimes contain, to put it nicely, surprising snippets of code, such as parsing the code of a function with regular expressions to see whether it contains certain references. The following two chapters, one on regular expressions and the other on timers, contain a lot of very useful background information, and practical tips and tricks.

The rest of the book, though, is a bit on the difficult-to-read side. The topics are either not really practical because they concern deprecated features (the ‘with’ statement has its own chapter), or explain in detail how to build a parallel implementation of browser features. These parallel implementations have one single reason: IE. Every other section in the second half of Secrets of the Javascript Ninja starts with “And then there is IE”, before explaining how to build a custom event-handler, CSS selector, or DOM manipulator. Other browsers also have bugs and errors, but IE appears to be a prick out of principle, not implementing central APIs, naming attributes and methods differently, and simply being buggy as hell. The sections on these parallel implementaions and bug-circumventions made me happy that I can just drop in jQuery, and ignore the ugly details.

Despite solid content on the language itself, I think Secrets of the Javascript Ninja would have been better if the chapters on tricking out the platform were a bit more limited on detail, and the programming techniques themselves were given more weight. Nevertheless, this is a great book for getting to grips with the language that is, unfortunately, the most widely used of all programming languages.