We were having a discussion, the other day, on the jQuery-dev mailing list concerning style and jQuery code. There was some discussion about how it could be improved.
One of the points discussed was concerning the use of callbacks and jQuery(this). Callbacks (passing in a function as the final argument to a jQuery method, to be called later) are used all throughout jQuery code. It’s a rather core aspect of jQuery (especially the use of closures to pass around information).
We began to wonder: What would jQuery look like if there were no callbacks? A couple solutions were proposed but I posited one that, I think, promotes the idioms present in jQuery.
Its use is simple (although its implementation is definitely a mind-twister). Instead of using a callback you now using jQuery chaining to execute all the methods that you need.
Where previously you would’ve done:
You would now write:
The end result is quite interesting. It definitely moves jQuery farther into the land of a “Domain Specific Language.” I wanted to put this code out there for people to try out and play with even though I have a number of reservations:
- I’m really not a fan of passing in a method name as a string argument, to another method. This is used a lot in Prototype.js but it just never sat right with me.
- The .chain() method name probably isn’t right. Some other name probably fits better here.
Nevertheless I think the result holds a lot of potential even if just as a proof-of-concept.