I just finished watching John Resig’s talk titled “Advanced jQuery” which he gave at Ajax Experience. Like I said before, I am posting notes which I took while watching the video, so that I can refer to them at a later point of time (It is easier to search when it is not on paper 🙂 ). Also it might help someone to get the outline of the talk before actually watching the video. The original video runs for more than an hour.
Also be warned that the following is my own interpretation of the video and I might have missed or could have interpreted some point differently. 🙂
About the speaker, John Resig
As you all know John Resig is the creator of the excellent jQuery library. He works for Mozilla corporation and you can get more information about him from his blog.
Some Advanced features of jQuery
Chaining
The most useful and the powerful feature of jQuery. (Personally this is my favorite feature in jQuery). Every function in jQuery returns the jQuery object and you can continue to call functions on that object. end()
stops chaining andSelf()
adds the current element into the chain.
Isolation
Once of the design goal of jQuery is that, jQuery should not affect or get affected on any HTML page, no matter what is present in the HTML page, either another library or even another version of jQuery itself. The only exception is when a new property is added to object.prototype.
Isolation in jQuery is achieved by the following
jQuery Wrapper
Everything in jQuery is wrapped. Only the $ function and the jQuery variable are exposed.
Plugin Wrapping
All Plugin code should be wrapped between
(function($) {
})(jQuery);
noConflict
Even the global jQuery variable can be unrolled by using jQuery.noConflict(true)
. This allows two versions of jQuery to run on a single HTML document without any conflict.
Sizzle
Sizzle is a selector engine written by John in pure JavaScript. It is around 1.5x to 4x faster and the major advantage of sizzle is that it can be integrated with other libraries, since it doesn’t have any dependency on jQuery.
Sizzle works from backwards while selecting and is therefore faster in most cases. It also caches the DOM tree and frees it by listening to the DOMAttrModified, DOMNodeInserted and DOMNodeRemoved events.
Sizzle has a separate homepage and you can get more information from it’s homepage.
DOM Manipulation
By far, most time of a script execution is spent only in DOM Manipulation.
jQuery automatically converts single tags to double tags using the following function. (I never know this before. The function seems to be very precise and well thought of)
elem = elem.replace(/(<(\w+)[^>]*?)\/>/q, function (all, front, tag) {
return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ? all : front + "></" + tag + ">";
}
jQuery 1.3 uses document Fragment for DOM manipulation and is much faster when compared with older versions.
Events
In jQuery you can bind event to any JavaScript Object.
Namespaced Events
From jQuery 1.2, Plugins can use namespaced events. The major advantage in using name spaced events is that it is very easy to clean up later and uses less memory space.
Special Events
From jQuery 1.2.2 some special events are available for easy event handling.
Events like mouseEnter, mouseLeave and mouseWheel are not actual events but are exposed by jQuery. jQuery does the internal implementation for these events.
Video
So my dear readers what you think about my notes. Also let me know if you like notes for videos in this format. If there is a demand, then I can post some of my notes on other videos which I have already viewed.
John Resig’s blog is an must read blog for all javascript programmers. It has nothing but top notch content.