var myVar = $( [] );
What does this jQuery do?
Does it initialize the variable to an empty jQuery set? I have searched the jQuery docs but haven’t found an explaination for this syntax.
Excerpt from the jQuery docs http://api.jquery.com/jQuery/
Returning an Empty Set –
As of jQuery 1.4, calling the jQuery() method with no arguments returns an empty jQuery set (with a length property of 0). In previous versions of jQuery, this would return a set containing the document node.
So could $( [] ) be a legacy method for returning an empty jQuery set or does it do something entirely different?
While there are different ways to interpret “legacy” (as in pst’s comment), the expression is precisely documented. We know that the
$function is the same asjQueryso looking upjQuerywe see it can accept arguments in several different forms:jQuery( selector, [context] )jQuery( element )jQuery( elementArray )jQuery( jQueryObject )jQuery()The one accepting an element array is documented like so:
This answers your question “So could
$( [] )be a legacy method for returning an empty jQuery set or does it do something entirely different?” in the sense that no, it does not do anything different. You get a jQuery object with all the elements in your empty array. All zero of them.And as you mentioned in your question, as of 1.4, plain ol’
$()gives you the empty set and so I take it that it may be the preferred way to do things now. That said,$([])is nice and explicit, and still supported.