Code:
a = $(document.body);
b = $('body');
a === b; // false [despite them appearing to be exactly the same]
"a === b" is false because you are comparing two different object pointers. The return value of a call to jQuery is a jQuery object, not a DOM node, so a and b are indeed different objects, even though a[0] and b[0] reference the same DOM node, and therefore reference the same object pointer.
Code:
// but...
a[0] === b[0] // true
a[1] // undefined
b[1] // undefined
Remember that the return value of a call to jQuery is an instance of a jQuery object. It is essentially an Array with a bunch of jQuery specific methods attached to it. Since there is only one <body> tag in the document, the jQuery collection object should only have a value at index zero.
Also:
Code:
a.length === 1; // true
b.length === 1; // true
You don't even need to use a.each(function() {}); to iterate over a jQuery collection. Any loop will do.
Code:
var i = 0, length = a.length;
for (i; i < length; i++) {
alert(a[i]);
}
Bookmarks