Prototype: Sliced Bread, or Devil’s Spawn?
People like to bitch about prototype.js, usually because it extends Array and Object in ways that are incompatible with a lot of existing code, including other add-on libraries. I fully agree with this criticism, but I have yet to find another alternative. Prototype fills a very important niche in the javascript ecosystem: a small, simple, succinct library that lets you do 95% of the things you’d want to do in a modern Ajax app.
Here are some other javascript toolkits. Mind you, these are not all trying to provide equivalent functionality. But I think it’s fair to include them all on the same list, because they are all trying to provide tools to make Javascript a more powerful language, and one more suitable for creating the types of web apps that most people want to make these days.
Mochikit is the one I’m most familiar with. It’s a great toolkit, and kind do all kinds of cool stuff that Prototype doesn’t. But, most of the time I just want to do a simple ajax call, or really minor DOM manipulation. Mochikit just feels like massive overkill in those cases.
This could be intention - Mochikit is supposed to be a heavy-weight solution, which is what you need sometimes. But being heavy-weight means that it’s got that namespaced, heavy-handed, verbose feel that I associate with Java and Python. For example, look at what you need to do to make an Ajax call in Mochikit:
var deferred_obj = MochiKit.Async.doSimpleXMLHttpRequest('/some/url');
deferred_obj.addCallback(updateDiv);
function updateDiv(result) { $('mydiv').innerHTML = result; }
Compare to Prototype:
Ajax.Updater('mydiv', '/some/url');
Aside: I’m no Mochikit expert, though I’ve used it on a few projects. If someone has a better way to write the example above, please leave a comment.
Aside 2: The technique above could more correctly be called AHAH.
You could go on and on about how Mochikit is more powerful, that its deferred object framework offers all kinds of possibilities for unusual ajax calls and other kinds of javascript magic. All that is true. But then there’s the fact that, 99.9% of the time, all you want to do is fire off an ajax call and update a div, or maybe call a function to process the results.
Easy things should be easy to do. When a “powerful” tool makes the easy case hard, I get suspicious. For code, easy means easy to write (which means, easy to remember: I don’t want to reference the docs every time). But it also means easy to read and understand later, and for that you want to be succinct and explicit. Reading the prototype code I can see that the line has something to do with ajax and something to do with updating. Simple and unmistakeable.
The best tools are ones that make the easy case easy, but still offer the power to do more complex and unusual things if you want to dive in a bit deeper. So I guess you could make a Prototype-style wrapper for Mochikit, with simple helper classes like Ajax.Updater to export a succinct interface to the powerful concepts that Mochikit offers, and that should be the best of both worlds.
As for Prototype, I’m certainly not enamored with it. In fact, I’m really hoping for is some other small library will come along and fill the same niche as Prototype, but do it better. In the meantime, Prototype is the best in that niche, so I’ll keep using it.