As a continuation of my earlier post on Ajax toolkits, I’d like to talk about jQuery. Over-all I am very impressed with jQuery and I wouldn’t hesitate to recommend it.
Extending bases classes
I have already talked about the perils of extending base classes. I am pleased to say that, like MochiKit, jQuery does not extend base classes. For me, this issue alone disqualifies Prototype and MooTools from serious consideration.
Self-contained
Unlike all other libraries I’ve discussed, jQuery is very self-contained. This is important for similar reasons why extending base classes is bad. Libraries that create many global functions pollute the global namespace and introduce the risk of conflict with other libraries or your own code.
In jQuery, everything is inside the “jQuery” namespace, plus a single global object called “$”. The “$” object is simply an alias for “jQuery”, so one could write:
jQuery(document).ready(function() {
// do stuff when DOM is ready
});
or…
$(document).ready(function() {
// do stuff when DOM is ready
});
This global “$” CAN introduce conflicts with other libraries like Prototype and MooTools which also want to use that symbol. Fortunately, you can tell jQuery to not touch the “$” symbol, using the jQuery.noConflict() function:
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
jQuery.noConflict();
// Use jQuery via jQuery(...)
jQuery(document).ready(function() {
jQuery("div").hide();
});
// Use Prototype with $(...), etc.
$('someid').style.display = 'none';
</script>
To me, this shows that the jQuery teams takes compatibility and code safety seriously.
The character of the library
I have argued against libraries that try to make JavaScript look like some other language. I’m glad to say that this is not the case for jQuery. That said, it does have an interesting approach that I rather like. True to its name, jQuery has a focus on querying and manipulating the DOM. You can search for objects by class, CSS3 styles, name etc. The way it does this is quite impressive:
$("p.chip").addClass("foo").removeClass("bar").show("slow");
With jQuery you can chain methods together because every method returns the same jQuery object with all the same methods. So you can make arbitrary chains of methods. Very neat. Here are a couple more examples:
// Example 1
$("#orderedlist").find("li").each(function(i) {
$(this).append(" BAM! " + i );
});
// Example 2
$("li").not(":has(ul)").css("border", "1px solid black");
I like these features a lot. This is different than typical JavaScript, but I don’t see it as trying to change the character of JavaScript but rather trying to improve access to the DOM. The DOM is the most annoying thing about JavaScript programming and really it has nothing to do with JavaScript itself. I don’t hold the DOM in the same high regard as JavaScript and I’m very glad to see jQuery making it less painful.
Documentation
It is difficult to judge documentation without writing a substantial program. Hence, the reader should take my comments with a grain of salt: jQuery appears to have excellent documentation. The library looks extremely simple and clear, so less documentation is needed to begin with. Yet, the jQuery team has a good API reference and a multitude of tutorials. I was impressed by the apparent diversity of information available.
Conclusion: jQuery is a fairly unique library in that it is so focused on making the DOM easier to manage and largely ignores other aspects of JavaScript programming. This isn’t bad. I fault the DOM for 90% of the pain of doing Ajax. I really like the library, I think that the DOM-manipulation functionality they bring is very impressive and extremely useful. And the authors seem to take compatibility and robustness seriously.
Note: I currently use Dojo for my work and after this evaluation I’m seriously considering adding jQuery to my programs. Too bad it doesn’t have all the GUI widgets I need. I’ll be watching the
jQuery UI project.