I jumped throught the different areas of jQuery source that are called when you type:
$('.foo')
or
$('#foo')
to try and determine how jQuery parses the selector ( I assumed charAt() ) but wanted to verify.
I got to here:
if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this;
}
But I got kind of stuck on what
selector.nodeType
does. This reference says that a nodeType can be pretty much anything…so what exactly are they checking for?
The jQuery API breaks down the possibilities further.
In summary what is this code snippet trying to accomplish regarding the selector variable?
nodeTypesuggests that the object passed to the jQuery selector is a DOM node (which will generally be an element). This allows, for instance, the following construction:documentis an object that represents the document.$(document)builds a jQuery object based on that element. The test fornodeTypemeans that jQuery can detect whether the argument was an element, and if so simply to build the selection based on that.You can also see this with the common construction
$(this):