JavaScript Ninja Secrets

Secrets of the JavaScript Ninja by John Resig and Bear Bibeault

This is a very good book which is so much more than a simple JavaScript language text. Sure, it covers some of the subtle points of the JavaScript language, and you are expected to have knowledge of JavaScript before reading the book, but it also offers a lot of information about some of the practical problems you will meet as a JavaScript programmer.

It starts in a slightly odd manner. There is a chapter discussing the benefits of cross browser development, followed by a chapter in which the  author writes a simple testing framework.  This framework allows you to embed tests inside an HTML page, and displays the results of the assertions inside the tests in an easy to read manner. Lots of the explanations that follow in the book will be expressed as assertions in the language of this testing framework. This way of working works very well.

The next section of the book, Apprentice Training, consisting of six chapters, has five chapters that focus on the subtle parts of the JavaScript language. The book focuses on the functional nature of JavaScript, discusses why it is important and then gives a very good explanation of scope and the four ways that you can invoke a function object – as a function, as a method, as a constructor and via apply and call. There is a good discussion of recursion followed by an example of memorisation, showing the big benefits of pure functional code. This is followed by a chapter on closures, which describes what they are and then shows how they can be used to implement partial application and temporary scopes with private local variables. We then go object-oriented in a discussion of prototypes and how they can be used to get features like those of standard OO languages. The author defines a JavaScript mini-framework that defines inheritance hierarchies that support a super operation to access base types. There is then a chapter on regular expressions, showing their great power (though you’ll need to be responsible to use them).

The last chapter of this section talks about the single threaded nature of browsers, the typical JavaScript host, and discusses how timers can be set and cleared. There is also lots of cross browser detail in how these things are implemented, and the book covers some of the common gotchas.

The next section, Ninja Training, first talks about runtime code evaluation, at first using eval and Function to make new function objects, but then moving on to describe the inbuilt decompilation (as you can ask a Function for its source code). There is then an interesting section showing some of the uses of these techniques.

The next chapter looks at with statements, a feature of JavaScript that you either love or hate. There is a small concise example that shows a powerful micro-templating engine that is written in a tiny amount of code, which shows the power of the features we have been shown before.

There are then two chapters on cross-browser strategies for using the DOM. Lots of practical discussion of the differences between browsers which taught me a lot about the DOM and how it should be used.

The last section, Master Training, also looks at browser differences, looking at the differences in the event model and CSS selectors.

The book is a really good read. It explains the parts of JavaScript that it covers really well, and you’ll learn loads about browsers and the DOM which is very interesting and useful if you do cross-browser work. The browsers that it discusses are a little behind the times, but that is probably the only complaint I have.

This entry was posted in Books. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s