问题描述
我正在查看的某些代码中包含以下内容:
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
更新:
还有更多代码:
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();
});
};
我仍然看不到第一行的要点.有人向我解释说,它创建了一个空的jquery对象,但我不明白为什么.
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.
推荐答案
其他答案正确.调用$()
会创建一个空的jQuery对象.
The other answers are correct; calling $()
creates an empty jQuery object.
但是,在您现有代码的上下文中,似乎不需要它.用$()
初始化的dialogs[id]
变量仅重新分配了另一个值,而不使用其原始值.
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.
需要注意的一件事是,在完成AJAX调用期间,随后会给dialogs[id]
变量赋予另一个值 ,这意味着该变量可以在您的其他位置使用异步操作$.get
正在进行时的代码.它可能在函数内部,但是使用var
的作用域不正确,因此有点可疑.
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.
这篇关于有人可以解释什么= $();在jQuery中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!