I have seen the following two variable initializations to create an empty jQuery object. Is there a major difference or advantage to use one over the other?
var a = $({});
var b = $();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you meant
$([]), that’s something from the old days where calling$()was actually equivalent to$(document)(which was an undocumented feature). So to get an empty set, you’d have to call$([]). This was changed in jQuery 1.4; the documented functionality of$()is now to return an empty set.Passing objects to the jQuery constructor is an entirely different beast.
$({})doesn’t create an empty jQuery object. It creates a jQuery object with a length of 1; the selected item is the object itself.Passing JS objects to the jQuery constructor lets you take advantage of a more esoteric feature of jQuery: binding and triggering events on (non-DOM) objects.
For example:
Either way, if your goal is to instantiate a new, empty jQuery object, use
$().