开发者

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

var myVar = $( [] );

What does this jQuery do?

Does it initialize the variable to an empty jQuery set? I have searched the jQuery docs b开发者_开发知识库ut 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 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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜