Before answering this question, you need to understand that $ (...) returns a special object with properties such as an array. From the documentation:
JQuery factory function The $ () function returns a jQuery object that has many array properties (length, array access operator [], etc.), but is not quite the same as the array, and some missing built-in arrays methods (such as .pop () and .reverse ()).
So here is what happens.
Regardless of the type of selector, new jQuery.fn.init( selector, context, rootjQuery ); , therefore, a new object is created and its properties are set in this constructor init() .
The argument $ (...) is tested for void, DOMElement, string or function (via isFunction)
All checks fail, and as a result, the following code is called.
if (selector.selector !== undefined ) { this.selector = selector.selector; this.context = selector.context; } return jQuery.makeArray( selector, this );
The makeArray () function turns the provided selector into an array-like object by simply setting the this.length property and this.ret[0]=..., this.ret[1]=... so that it "feels" like an array while it remains an object based on the jQuery prototype (aka $.fn ). Remember that this in the code above is just a new object with $.fn in the prototype. In addition, the .selector and .context copied from the original to this.
It is important to note that, for example, the .prevObject Property will be lost, but a new one may be added depending on the method chain.
In general, it takes time and memory, and it probably makes sense to just check .selector when someone can pass the jQuery object to your plugin. On the other hand, a new instance with the same behavior as the original may be useful.
source share