Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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?

like image 202
Duncan Gravill Avatar asked Aug 20 '11 18:08

Duncan Gravill


People also ask

What does $( this mean in jQuery?

$(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.

When we should use $( this in jQuery?

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.

How do you check if an input is a string in JavaScript?

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.


2 Answers

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:

  • elementArray: An array containing a set of DOM elements to wrap in a jQuery object.

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.

like image 199
Ray Toal Avatar answered Oct 23 '22 07:10

Ray Toal


$(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
like image 37
Anthony Sottile Avatar answered Oct 23 '22 06:10

Anthony Sottile