Reputation: 32758
I have the following in some code I am looking at:
// Save an empty jQuery in our cache for now.
dialogs[id] = $();
Can anyone explain what it means. I have no idea about this concept. thanks
Update:
Here's a bit more of the code:
var loadAndShowDialog = function (id, link, url) {
var separator = url.indexOf('?') >= 0 ? '&' : '?';
// Save an empty jQuery in our cache for now.
dialogs[id] = $();
// Load the dialog with the content=1 QueryString in order to get a PartialView
$.get(url + separator + 'content=1')
.done(function (content) {
dialogs[id] = $('<div class="modal-popup">' + content + '</div>')
.hide() // Hide the dialog for now so we prevent flicker
.appendTo(document.body)
.filter('div') // Filter for the div tag only, script tags could surface
.dialog({ // Create the jQuery UI dialog
title: link.data('dialog-title'),
modal: true,
resizable: true,
draggable: true,
width: link.data('dialog-width') || 300
})
.find('form') // Attach logic on forms
.submit(formSubmitHandler)
.end();
});
};
I still cannot see the point of the first line. It's been explained to me that it creates an empty jquery object but I can't understand why.
Upvotes: 0
Views: 907
Reputation: 14747
The other answers are correct; calling $()
creates an empty jQuery object.
In the context of your existing code, though, it seems that it's not needed. The dialogs[id]
variable, which was initialized with $()
is only reassigned with another value, without using its original value.
One thing to take note of, however, is that the dialogs[id]
variable is subsequently given another value during the completion of an AJAX call, which means that it may be used somewhere else in your code while the asynchronous operation $.get
is going on. It may be inside a function, but it's not properly scoped using var
, so that's a bit dubious.
From what it looks like (and based on how its used) though, I'm willing to bet that it's not, and you may be correct that the $()
initialization is completely unnecessary.
Upvotes: 2
Reputation: 106027
From the docs (you did check the docs, right?):
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 of0
).
Upvotes: 2
Reputation: 149524
It creates an empty jQuery object. This object contains no actual elements, but it does have all the jQuery functionality added to it.
var $el = $();
// later you can do…
$el = $el.add('body'); // same effect as if you’d written `$el = $('body');`
Upvotes: 3