first_page

Flippant Remarks about jQuery

My tendency is to not accept learning by rote. It is important for me to compose a particular subject into a larger context. These “artistic” compositions make learning visionary so that I can see understanding. So, for me, jQuery is composed with CSS technology. jQuery is the answer to the question, “I really like the way CSS declaratively selects DOM elements, why can’t I do this in JavaScript?” I’m almost sure that Mozilla’s John Resig explains this CSS connection in his Google Tech Talk. But he also might touch upon this in his Yahoo! YUI Theater presentation. I really can’t remember where on the Web this is right now.

What’s JavaScript like without jQuery? Here is a sample of my YUI-based code:/*jslint browser: true, cap: false, glovar: true, passfail: false, undef: false, white: false */

/*global SONGHAY YAHOO */

(function(){

//Module-level members: var client = SONGHAY.ui.Client;

SONGHAY.ui.Index.displayBuilder = { … forDocumentLinks:function() { //Format links: var o = window.document.links; for(var i = 0; i < o.length;="" i++)="" {="" if(o[i].innerhtml="" &&="" (o[i].innerhtml.tolowercase().indexof('img')="=" -1))="" {="" if(o[i].classname="" &&="" o[i].classname="=" 'tabanchor'){}="" else="" {="" client.ui.eventcore.seteventbymetadataid('indexlink',o[i]);="" }="" }="" }="" },="" fordocumentscrollbars:function()="" {="" format="" scroll="" bars:="" var="" o="YAHOO.util.Dom.get('DocumentPageKbSubjects');" if(o="" &&="" o.style="" &&="" o.style.scrollbarbasecolor)="" {="" o.style.scrollbarbasecolor='#8bd63d' ;="" }="" o="YAHOO.util.Dom.get('DocumentPageKbArticle');" if(o="" &&="" o.style="" &&="" o.style.scrollbarbasecolor)="" {="" o.style.scrollbarbasecolor='#6c6c6c' ;="" }="" },="" …="" module-level="" "this":="" var="" self="client.ui.Index.displayBuilder;" })();="">

Buy this Book at Amazon.com!I am certain that an experienced jQuery developer can replace my forDocumentLinks() function with its for loop with an efficient jQuery line or two. It is my understanding that jQuery essentially eliminates the need to loop through elements to make changes to them. Also that YAHOO.util.Dom.get construct in my forDocumentScrollBars function is either deprecated or completely eliminated in YUI 3.x.

The YUI team at Yahoo! has recognized Resig’s efficiency here and even the mighty Doug Crockford openly admits that, “YUI 3 is doing this, sacrificing backward compatibility for a chance at a more glorious future.”

A savvy reader might ask in passing, “Okay why don’t you show me a rewrite of your code above using jQuery or YUI 3.x?” I look forward to exercising my time-management skills and rewriting all of my JavaScript for YUI 3. But right now is the more interesting question: Why did not use jQuery in the first place? My answer is a mixture of technical and social reasoning.

Technical Issue: jQuery was not built from the ground up to recognize ‘namespaces’

The relatively new jQuery documentation states that, “The jQuery library and virtually all of its plugins are constrained within the jQuery namespace. As a general rule, ‘global’ objects are stored inside the jQuery namespace as well, so you shouldn’t get a clash between jQuery and any other library (like Prototype, MooTools, or YUI). … That said, there is one caveat: By default, jQuery uses $ as a shortcut for jQuery.”

A younger more “courageous” programmer would be unconcerned with the use of the phrase “virtually all”—sadly, I failed to be so young and dashing.

Social Issue: “Documentation is for Girls”

It was an error to assume that just because John Resig was one dude—and a young dude at that—he would be like many of the other JavaScript kiddies out there making “code libraries” and save documentation for last. There is a self-centered, flippant cowboy style of pride in “undocumented” features that Macromedia made famous, that was hours of fun in my late twenties and early thirties when CD-ROM titles ruled the wired world. But now that I’m a little older and relatively wiser, I have no patience for whiz-kid antics. So, what the YUI team had from the beginning were professional habits. They have documentation, samples—and even test suites. This is the Warren-Buffet common sense reason why I started with YUI—even though Resig’s technology is now accepted (even by Microsoft) as technically superior.

It was my mistake for grouping Resig into a wacky summer camp in which he clearly does not belong. I discriminated against him because of his age and relative lack of resources. Time has proven Mr. Resig to be quite serious and authentically computer scientific.

rasx()