Finding the Good Parts
A few months ago, I finished reading the short, but ever-so-sweet, JavaScript: The Good Parts by Douglas Crockford. Crockford is mostly known for JavaScript Object Notation (JSON), JSMin, JSLint, insightful essays, and many contributions to the JS community.
I didn't used to respect JavaScript: it was a little kid's programming language. The first code I ever wrote was written in JavaScript and I was about 10 years old. I was making "really cool" image rollovers on Dragonball Z pics. It was sloppy.
When I first was hired at Western by University Residences last school year,
I hadn't done too much programming in the interim, just a CS elective class on
"dynamic" HTML. Even in that class, I was still handling events with inline
onclick
function calls. The ugliness of HTML, browser
inconsistencies, and the DOM all felt like having just slipped and fallen into
a puddle of cold mud. Real Programming™ was a vague, enigmatic practice
that people with magical powers performed; this sure wasn't it.
As it turns out
All my previous experiences with JavaScript actually support Douglas Crockford's arguments. He justly proclaims JavaScript the "Wrrrld's Most Misunderstood Language" and he isn't an advocate of JavaScript, so much as an advocate of a core subset of the JavaScript language. Eventually, my opinion of JavaScript began changing.
When I began working at University Residences and Robbie showed me jQuery, JS became more hygienic. The inconsistencies between browsers disappeared; the DOM was abstracted away. JQuery provides a much cleaner API for selecting and modifying HTML elements.
I remember stumbling upon all of The Little Schemer's exercises in JavaScript, and even a scheme interpreter within the browser. My JavaScript paradigm was being challenged. What I had thought of as a toy, was really a double-bladed sword: powerful, yet tricky to wield and potentially dangerous.
It turns out that the man who wrote that scheme interpreter in JavaScript was none other than Douglas Crockford. I spent the next few days reading all of his essays on JavaScript, realizing I had to read his book as well. Luckily, University Residences is an awesome place to work, and DFrost, my manager, had the book in my hands within a university-week.
JStGP is an excellent book. It delivers exactly what it promises: short, sweet, good, JavaScript. A perfect primer in forgetting every bad habit and replacing them with knowledge and good practices.
JavaScript has C-style syntax, but shares little in common with other languages with that style of syntax. It's prototypal object system goes against the grain of classical inheritance. Function literals are the equivalent to Lisp's lambdas; real closures (ahem, Python, I'm glaring at you); lexical scope!
I realize now that Real Programming™ can be and is done in JavaScript. It is not a toy language, and it has become one of my favorite tools in the shed.
And while JavaScript can be fun, I am not blind to its glaring defects; neither is Douglas Crockford. He still finds many chances to gripe about the less than great portions of the language, even though the book is titled The Good Parts. I don't feel the need to repeat each of them, but I do feel that he is off on one point.
Objects in JavaScript: Almost
DC is a fan of the protypal object inheritance in JS:
I have learned to fully embrace prototypalism, and have liberated myself from the confines of the classical model.
I am a fan of prototypalism as well. Because JavaScript is an incredibly dynamic, weakly typed language that is very flexible, it will let you get away with almost anything before it will throw an error at you. Freedom. Freedom to write code how you see fit, employing whatever dirty little tricks you want without the language telling you "No."
This freedom can be either great or horrid and many people will give you many opinions. In the end, I prefer freedom, but this is a subject for another day.
Prototypes and object literals add to this freedom. Why should I need to statically define an object before I use it? What if my code needs to create arbitrarily defined objects on-the-fly? JavaScript gives me the flexibility to do just that.
However, as with many aspects of the language, JavaScript doesn't quite get prototypes right. Object literals are dead on perfect, but the prototypal system doesn't pan out like it should. DC doesn't take it far enough: prototypalism in JavaScript isn't as pliable as promised.
John Resig, author of jQuery, wrote the following example for his upcoming book, Secrets of the JavaScript Ninja, and posted it to his blog with a bunch of other code that will probably also be in his book.
function Person(){}; Person.prototype.dance = function(){}; function Ninja(){} // Achieve similar, but non-inheritable, results Ninja.prototype = Person.prototype; Ninja.prototype = { dance: Person.prototype.dance }; assert( (new Ninja()) instanceof Person, "Will fail with bad prototype chain."); // Only this maintains the prototype chain Ninja.prototype = new Person(); var ninja = new Ninja(); assert( ninja instanceof Ninja, "ninja receives functionality from the Ninja prototype" ); assert( ninja instanceof Person, "... and the Person prototype" ); assert( ninja instanceof Object, "... and theObject prototype" );
The cases that don't produce inheritable results are counter-intuitive and diminish from the extensibility and power of JavaScript's objects. There is no excuse why they shouldn't work and this is (another) fault of the language.
I haven't committed myself to learning Self or NewtonScript, the only protypal language I have delved into besides JS is Io, but it feels so right. Prototypes are so easy, and feel more powerful than they ever do in JavaScript. I would be very interested to hear Crockford's opinions on Io.
In the end
JavaScript continues to evolve, as do my assessments of the language. Some love it, some revile it; even Brendan Eich, the father of JS, still isn't pleased:
Ok, back to JavaScript popularity. We know certain Ajax libraries are popular. Is JavaScript popular? It's hard to say. Some Ajax developers profess (and demonstrate) love for it. Yet many curse it, including me. I still think of it as a quickie love-child of C and Self. Dr. Johnson's words come to mind: "the part that is good is not original, and the part that is original is not good."