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?
$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.
this would refer to the current DOM element h1 which invoked the event. Enclosing it with a $ makes it a jquery object. So $(this) basically refers to the jquery h1 object . So $(this) = $('h1') where h1 is the event which triggered the event.
Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string') . If the typeof operator returns "string" , then the variable is a string.
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 as jQuery
so looking up jQuery
we 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.
$(obj)
allows you to create an empty jQuery object, but this is different from $()
For example: I have a namespaced solution that needs to have custom events that take action based on a centralized object. I've written my own EventHandler
class which basically wraps around a private $({})
This allows me to have an object that can act as my event holder and triggerer. I expose bind(), unbind(), and trigger() in my EventHandler
class.
$()
on the other hand does not allow you to bind and trigger as there is no base object. I'm not entirely sure their ideas with doing this as it would make more sense to have the "default" or "empty" jQuery object to be $({})
Long story short: there are things that $({})
does that $()
does not. But $([])
and $()
seem to act the same from the minimal amount of testing I've done. Perhaps $(new Array())
acts differently still, but I don't care to research it.
EDIT with example:
var testA = $();
testA.bind('customEvent', function () { alert('empty custom'); });
testA.trigger('customEvent'); // will not get an alert
var testB = $({});
testB.bind('customEvent', function() { alert('object custom'); });
testB.trigger('customEvent'); // will get an alert
var testC = $([]);
testC.bind('customEvent', function () { alert('array custom'); });
testC.trigger('customEvent'); // acts like testA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With