Tuesday, September 11, 2007

Book review: Learning JavaScript, by Shelley Powers

I don't usually bother with book reviews, but Learning Javascript, by Shelley Powers, is an exceptionally bad technical book.

I could write a page-by-page critique, but I'll limit myself to five things that illustrate the problems with this book:

  1. Most blatant, the publisher's confirmed errata runs to 9 printed pages. It includes 83 distinct items. That's an acknowledged error every 4 pages.

  2. The book is full of imprecisions and subtle errors that aren't captured in the errata. For instance, on page 221, the book states "Instead of using prototype, I could have added the trim function directly to a string instance (variable)... Only if the property or method is not found in the global object as part of the basic object or via the prototype collection, does the engine search for it locally, attached to the variable."

    There is an important distinction between a variable and its contents. But the author first conflates them, saying "...instance (variable)...", and then says "variable", where in both cases "instance" is correct and any mention of "variable" is wrong.

    Oh, and by the way, the order described is back-asswards: in actuality, local properties trump prototype properties, not the other way around.

  3. Here is an excerpt from the much better book, Javascript: The Definitive Guide, 5th edition by David Flanagan:

    When the JavaScript interpreter starts up, one of the first things it does, before executing any JavaScript code, is create a global object. The properties of this object are the global variables of JavaScript programs. When you declare a global JavaScript variable, what you are actually doing is defining a property of the global object.

    The JavaScript interpreter initializes the global object with a number of properties that refer to predefined values and functions. For example, the Infinity, parseInt, and Math properties refer to the number infinity, the predefined parseInt( ) function, and the predefined Math object, respectively...

    In client-side JavaScript, the Window object serves as the global object for all JavaScript code contained in the browser window it represents. This global Window object has a self-referential window property that can be used instead of this to refer to the global object. The Window object defines the core global properties, such as parseInt and Math, and also global client-side properties, such as navigator and screen. ...

    JavaScript implementations may allow multiple global execution contexts, each with a different global object. (Although, in this case, each global object is not entirely global.) ...Client-side JavaScript code in each frame or window runs in its own execution context and has its own global object. However, these separate client-side global objects have properties that link them. Thus, JavaScript code in one frame might refer to another frame with the expression parent.frames...

    Unless I really missed something, the "global object," its relationship to scope, and the possibility of having several instances of it at once, are never clearly discussed in Learning JavaScript. Certainly the "global object" doesn't appear in the discussion of global variables; neither is it mentioned in the chapter on objects; nor is it mentioned in the index. The section on frames mentions the use of "parent", but doesn't explain that "parent" is, again, a property on the local "global object."

    Which brings me to my next item:

  4. The index is barely 7 pages long. In its brevity it omits innumerable important keywords, functions, and concepts.

  5. Finally, the book is poorly organized. For instance, the section on error handling appears in the middle of the chapter on "Creating Custom Javascript Objects."

Overall, if you make it through Learning JavaScript you'll probably be able muddle through some JavaScript, but you're not going to have a good mental model of what it is you're actually doing. I say go straight for Javascript: The Definitive Guide: it's well-written, has great technical accuracy, and is much more comprehensive than Learning JavaScript