Thursday, August 8, 2013

The Bling Chain

JQuery is the most popular JavaScript toolkit. But I think it's inherently flawed, and there are much better alternatives.

JQuery is a toolkit for manipulating the Document Object Model (DOM), i.e. the live editable tree structure that the browser creates from a webpage in its memory. It is useful for doing stuff like this:


The $ (bling) function looks for an element (crotch) in the DOM, and applies some functionality (sniff) to it. Since that function just returns the same element (crotch), you can run it again through another function (scratch). As a matter of fact, every other function returns the same element, so you can create a whole chain of functions (the bling chain). This means that the toolkit extends the element with some custom behavior. Nothing wrong with learning an old dog some new tricks. However, JQuery adds all this functionality in one single dirty file. In fact, JQuery just fails on the nice script above, because it doesn't have any of the tricks in store that I desire.

I could write my own script to extend the basic functionality of JQuery, but that's all very ad hoc. Since I have no way of telling what behavior is currently in JQuery, I'm sure to run into trouble later. I have to manage my extensions in some way as to make them compatible, and this basically means writing a lot of dollar signs.

Alternatives are not always easy to learn, and the backing of JQuery by the community makes it hard to switch. However, DomScript, a stack-based DOM manipulating script built in Clojure, makes for a nice example of how a very clean alternative can be created. Unfortunately it cannot be used in the browser.

Acuna was also created to do JQuery-like DOM manipulation. It is also stack-based, and can be used in the browser. Acuna looks a lot like JQuery, and can even be customized to use the same function names, and because of this, you don't have to know how a stack-based language works to use it. But at the core it has a very powerful concept, and is more than just another toolkit. In addition, every function in Acuna is read from a separate module, so managing modules is very easy.

Bling Chainers beware the walk-in wardrobe scam: JQuery is a scurvy dog in designer clothes.