JavaScript: The Good Parts
by Douglas Crockford, Dec 2008, 978-0596517748
It is interesting that I never really picked up JavaScript seriously before, though it has been the language of the web for over 10 years. By serious, I mean of course I have produced a few code snippets in it, but I’ve never even bothered to actually learn the syntax. You know, most people write JavaScript just like this, cut-n-past, trial-n-error. The perception of the language for most people is quite lame. Well, it turns out not to be lame at all.
This book to JavaScript is very much like Martine Fowler’s book to UML. I liked UML Distilled so much. I prefer the approach of learning by the best yet small parts first, at least as a start. Not only the learning curve is made more appealing, but those not-so-useful parts are avoided if we don’t ever use them. Why bother if I never used diagrams other than class, activity, or sequence diagrams in UML? Wouldn’t it be great to just learn how to use those 3 diagrams?
In this book, Crockford talks about JavaScript only. No DOM, no browser, pure JavaScript, simply the language itself. He also only focuses on a subset of JavaScript. He calls this subset “good parts”, which is a selected set of constructs and concepts in the language he considers good for programming. So what are the good parts of JavaScript?
- loose typing
- expressive object literals
- functions
- prototypal inheritance
Personally I think the most important concepts to grasp in JavaScript is the prototypal inheritance and functions. Chapter 3 has a great introduction to JavaScript’s object model and how prototype works. Chapter 4 introduces functions, including the function scope, closure, and the 4 kinds of function invocation pattern. The usage of constructor is also introduced in this chapter to let you understand what the following code snippet does and how it works:
var Quo = function (string) { this.status = string;};
Quo.prototype.get_status = function ( ) { return this.status;};
var myQuo = new Quo("confused");
document.writeln(myQuo.get_status( ));
Understanding this is vital to either pseudo-classical or prototypal inheritance, which are introduced in chapter 5. Crockford considers pseudo-classical inheritance as a “bad” part of the language and advocates the usage of prototypal inheritance or even functional style instead.
These 3 chapters are probably the most important ones in the book (chapters 3, 4, and 5). The rest mostly introduces various parts of the languages by mentioning what should be noted when using them. The style is more like “gotchas”. In the appendices, Crockford also lists the awful and bad parts of the language he thinks you should be aware of (the awful parts) and avoid (the bad parts).
This book is very good in helping understand the core parts of JavaScript. Though the book is recommended by most people, its intention (using only “good” parts of the language) is rather controversial. For example, some don’t agree that pseudo-classical inheritance as bad part, but some agree that given 2 ways of doing the same things, why choosing the confusing one. Nevertheless, it’s always a good thing to know the opposite sides before you make your mind.
I do have a slight complaint about the book. Some places are inconsistent across the whole book. For example, Crockford has provided a very useful method for creating objects with the ability to designate their prototype objects (so you don’t need fiddle with the use of “new” operator). However, most of the time he calls this method “Object.prototype.beget” in the book but in the provided source code it was named “Object.create” (which is quite confusing, isn’t it?). Checking from his Prototypal Inheritance in JavaScript, it’s obvious that many places mentioning “Object.prototype.beget” was not updated accordingly while introducing the improved version of that method into the book. The same problem also exists in the way he introduces to identify array object type, which is inconsistent between chapter 6 and appendix A.
Another one I found rather annoying is that his terse explanation is sometimes, well, too terse. For example, while mentioning the wrapping of JSON text from the wire in parentheses, he simply explains:
The concatenation of the parentheses around the JSON text is a workaround for an ambiguity in JavaScript’s grammar.
which was too terse in my opinion.
Overall I really like this book. It’s light but deep, and yet easy to grasp.


