O’Reilley just published a new book by well-known JavaScript Guru Douglas Crockford. JavaScript: The Good Parts is a excellent introduction to using JavaScript in an effective, maintainable manner, that will be easy to use and maintain into the future.
Crockford pulls no punches about the JavaScript in this book. He freely admits that it was released before it was ready, that there are a number of poorly considered, and some just plain dangerous features, especially in the wrong hands. He describes JavaScript as “Lisp in C’s clothing,” which is an apt description. JavaScript has hallmarks of both a Functional and a Procedural language, but the biggest dangers come from trying to use the Functional elements as if they were Procedural. Something which is far too easy to do.
JavaScript: The Good Parts was written for your average Procedural programmer (ie, most of us). It isn’t about the DOM, it isn’t about writing Web Applications. It’s about using JavaScript as an effective programming environment. He begins by running through the grammar of the language, giving a thorough overview of all the features of the language.
This is followed with a discussion of JavaScript’s Object model. JavaScript’s objects are dangerous because it seems enough like a Object-Oriented language to be dangerous. The prototype system is unlike any other object system, and while it can be used as if it weren’t, doing so tends to be incredibly slow. Luckily, JavaScript objects, due to JavaScript’s functional nature, are incredibly powerful, allowing complex data models to be built, easily, and every object can be easily extended to add new functionality. In fact, a fair amount of the book are convenient methods to be added to the core objects to add functionality that probably should have been there in the first place.
Most useful to me, was a discussion on inheritance. In JavaScript everything is a global. Worse, members of a class are all public, so there isn’t any directly obvious means to keep variables private. Luckily, JavaScript has an answer for that. The closure. By writing a function that creates and returns an object, you can declare variables inside the function which will be accessible to functions inside the object you return. Better yet, they remain accessible as long as the object you return is in scope.
JavaScript has it’s weaknesses. But it is the language of the web. I don’t see that changing. Crockford’s book is a great introduction in how to use JavaScript effectively. Just because JavaScript has a reputation as a toy language, doesn’t mean that real engineering isn’t possible in it. The direction the web has taken in the last few years has really proved that JavaScript is a powerful language, with powerful tools. You just need to use it correctly.
If you’re touching the web at all, you owe it to yourself to buy this book and read it. Hell, read it a few times. It gets better each time you read it.