In jQuery what does ...... var myVar = $ ([]); ...... do?

var myVar = $( [] ); 

What does this jQuery do?

Does it initialize a variable with an empty jQuery set? I searched for jQuery docs but did not find an explanation for this syntax.

Excerpt from jQuery docs http://api.jquery.com/jQuery/

Return empty set -

As in jQuery 1.4, calling the jQuery () method without arguments returns an empty jQuery set (with the length property of 0). In previous versions of jQuery, this would return a collection containing the node document.

So can $( [] ) be an obsolete method to return an empty jQuery set, or does it do something completely different?

+7
source share
2 answers

Although there are different ways to interpret "legacy" (as in the pst comment), the expression is accurately documented. We know that the $ function is the same as jQuery , therefore, looking up jQuery , we see that it can take arguments in several different forms:

  • jQuery( selector, [context] )
  • jQuery( element )
  • jQuery( elementArray )
  • jQuery( jQueryObject )
  • jQuery()

The receiving array of elements is documented as follows:

  • elementArray: an array containing the set of DOM elements to transfer to the jQuery object.

This answers your question: "Could $( [] ) be an obsolete method to return an empty jQuery set, or does it do something completely different?" in the sense that no, he does nothing else. You get a jQuery object with all the elements in your empty array. All zero.

And as you mentioned in your question, starting with 1.4, a simple ol ' $() gives you an empty set, and so I think this may be the preferred way to do something now. However, $([]) is nice and explicit and is still supported.

+1
source

$(obj) allows you to create an empty jQuery object, but this is different from $()

For example: I have a solution with a namespace that needs to have custom events that take action based on a centralized object. I wrote my own EventHandler class, which basically wraps a private $({})

This allows me to have an object that can act as my event holder and trigger. I expand bind (), unbind () and trigger () in the EventHandler class.

$() , on the other hand, does not allow to bind and run, since there is no base object. I am not completely sure of my ideas with this, since it would be more reasonable for the jQuery object "default" or "empty" to be $({})

In short: there are things that $({}) does, that $() does not. But $([]) and $() seem to work the same way with the minimum number of tests I did. Maybe $(new Array()) acts differently, but I don't want to explore it.

EDIT with an 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 
+1
source

All Articles