JQuery: (Mis)leading the Pack

—Wednesday, August 23 2006

I always hate election season. It’s not really the 3 billion signs you see staked at every corner, it’s the negative campaigning and slight twist of the truth by opposing candidates. Running these types of campaigns makes you look foolish, and in turn, opens you up for scrutiny.

So what does this have to do with JQuery? Well, I just finished reading a post on the JQuery blog entitled “Why JQuery’s Philosophy is Better”(than Prototype). A prime example of failure to “Get your facts straight.”

Basically the guy calls Prototype ugly, states that it’s baked into Rails and wonders why more Rails developers don’t use JQuery considering it’s philosophy is more in-tune with your your average Rails developer. To top it all off, he posts some bad code comparisons between Prototype and JQuery.

DOM Insertion: Admittedly, if you use Prototype as-is, you have to do what the article says. But, the deal here is that you can get that same functionality with a few lines in Prototype. This should be in the Prototype core, I’ll certainly give them that much.

	Element.addMethods({
	  insertionBefore: function(element, content) { new Insertion.Before(element, content); },
	  insertionAfter:  function(element, content) { new Insertion.After(element, content); },
	  insertionTop:    function(element, content) { new Insertion.Top(element, content);  },
	  insertionBottom: function(element, content) { new Insertion.Bottom(element, content); }
	});

	$("item-1").insertionBefore("<li>Sweet</li>");

So you only have to add the addMehtods bits once and you can use it like the last line to your hearts content. I do have slightly more verbose function names, because ‘before’ and ‘after’ describe a place, they don’t fully disclose the action. They haven’t quiet reached the $(element).google() status yet.

The article also says that Prototype returns a vanilla DOM node. This is false, you get all the methods of the Element object and more.

Chainability: We got it. It works.

CSS Selectors/Xpath: We’ve got CSS selectors $$(), but I doubt we’ll ever see XPath support in Prototype. It’s not really something I see a need for (Speaking for myself of course).

Plugin Development: Extensions/Plugins/Etc. Javascript is Javascript, it’s just as easy to add plugins to Prototype as it is to JQuery. The difference here is that with JQuery you have to use a fully qualified namespace (it seems) and some weird looking functional programming. Just browsing through the JQuery plugin site, it seems all of the plugins use a functional approach. Despite the articles claims, this is as far from Ruby as you’ll get.

Automatic Looping: A big paragraph of how looping elements works with insertion. He says it’s one of the day to day benefits of using JQuery. We like this in the Prototype camp too:

	$$('span').invoke("insertionBefore", 'OMG');

Ajax Updating: Yeah, it sucks, it’s verbose. I agree.

Adding a class to an element:

$(element).addClassName('omg');

Adding a class to a group of elements:

	$$('span').invoke("addClassName", 'omg');

So, most of the talking points of the entire article are misleading as I’ve shown. It would be mighty square of them to correct the article.

I really don’t think the author is intentionally misleading, I just think know his knowledge of Prototype is modest at best. I won’t pretend to write JQuery comparisons to Prototype, because frankly, I don’t know JQuery enough to make an accurate comparison.

So this brings me to my last point. Does JQuery really pack anything new or revolutionary enough for Prototype users to swap? Has it really brought anything new to the table? After all, a lot of JQuery was inspired by Prototype. The main thing I see JQuery touting is file size and I’ve already had my say on that. It gets you a big bowl of alphabet soup (I hear John is addressing this though with the new version coming soon):

jQuery.parents = function(a){
	var b = [];
	var c = a.parentNode;
	while ( c && c != document ) {
		b[b.length] = c;
		c = c.parentNode;
	}
	return b;
};

Here’s the twister though. I hope JQuery continues to do great. It only means good things for Prototype. Jhon is a smart guy who has done an amazing job with JQuery and he continues to stay vocal in the community and let his users know exactly what’s going on, something Sam fails at miserably. I won’t speculate as to why thats the case.

So, yes, we do have our problems in the Prototype camp, the primary two being the complete lack of transparency about what’s going on and limited documentation. It has very little to do with the code being ugly, you just have to know how to use it well enough to not write ugly code.