views:

194

answers:

4
var username = $("#username"),
                password = $("#password"),
                allFields = $([]).add(username).add(password);

What is allFields? What is $([])?

Being a newbie to Javascript/jQuery, I've never seen this $([]) notation before and I'm interested in its associated methods.

Given that its "$([])", it's tricky to search for. And a Google search of arrays in Javascript (guessing that thing is an array of some sort) yields the typical arrays I'm familiar seeing.

So what is $([])? Can anyone point me to some documentation? I'm interested in learning how to use this strange thing.

+6  A: 

[] it's an empty array. Wrapping it with $() overcharges it with jQuery's functions.

In javascript you can create an array this way:

var foo = [];
fcingolani
Overcharges it with jQuery functions? Given your example, if you did `$(foo)`, your reference to `foo` is still just referencing a normal Array.
patrick dw
being strict, the result of $(foo) is overcharged, foo stays "normal".
fcingolani
"overcharge" is an unfortunate description for a dollar sign function.
Crescent Fresh
I'd agree with @Crescent. It doesn't really describe anything that's happening. All that's happening is the content of the Array being passed is being copied into the newly created jQuery object, which is effectively an Array (or an Array-like object).
patrick dw
I agree with him too. XD
fcingolani
+5  A: 

It is an empty array being passed in the creation of a jQuery object. Presumably to initialize something.

Shouldn't be necessary, at least not in the latest versions of jQuery.

Here's an example without the Array: http://jsfiddle.net/MAzLN/

Works fine, as the alert() shows one element in the object.

jQuery initializes the length property to 0 when the object is created, so I'm not sure what the purpose is, unless it was required in the past.

patrick dw
Whoa, very cool link! Thank you!
Tina D.
+3  A: 

I believe it creates an empty jQuery collection object.

CD Sanchez
To add to Daniel's answer, with older versions of jQuery you couldn't create an empty jQuery object with just $() so passing an empty array was necessary.
CalebD
+8  A: 

The jQuery function accepts an array of DOM nodes.

$([document.body]) for example which will return a jQuery object by wrapping all the DOM elements passed in that array. However, since in your example there is no DOM object to begin with and it's just an empty array, there is not need to even pass an empty array.

Calling the jQuery function without any arguments returns an empty set. Taken from jQuery docs,

Returning an Empty Set

As of jQuery 1.4, calling the jQuery() method with no arguments returns an empty jQuery set. In previous versions of jQuery, this would return a set containing the document node.

So, your example would work the same if you had instead called

$().add(username).add(password);

As other answers have mentioned and the docs say, passing an empty array was required before v 1.4.

Anurag
+1 Good answer. I forgot about it being new behavior that `$()` returns an empty object.
patrick dw
Thank you, this is what I was looking for. Excellent answer.
Tina D.
@Tina - Remember to "accept" this answer if you feel Anurag gave the clearest explanation. You accept by clicking the large checkmark to the left of it. :o)
patrick dw
I didn't want to jump the gun, but actually, this answer got me to where I needed to be. One green checkmark, coming up!
Tina D.