JQuery 中的 new 运算符意味着
我在互联网上发现了这段代码。它是关于实现打印功能的插件。我只是想知道调用该函数时 new 运算符意味着什么。
$.fn.printArea = function( options )
{
printWindow = new Popup();
writeDoc = printWindow.doc;
}
function Popup()
{
var windowAttr = "location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no";
windowAttr += ",width=" + settings.popWd + ",height=" + settings.popHt;
windowAttr += ",resizable=yes,screenX=" + settings.popX + ",screenY=" + settings.popY + ",personalbar=no,scrollbars=no";
var newWin = window.open( "", "_blank", windowAttr );
newWin.doc = newWin.document;
return newWin;
}
如果有人可以解释在 new 中调用弹出函数背后的原理,我将非常感激。
I came across this code in Internet. It is about a plugin that implements printing functionality. I am just wondering what does the new operator means while invoking the function.
$.fn.printArea = function( options )
{
printWindow = new Popup();
writeDoc = printWindow.doc;
}
function Popup()
{
var windowAttr = "location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no";
windowAttr += ",width=" + settings.popWd + ",height=" + settings.popHt;
windowAttr += ",resizable=yes,screenX=" + settings.popX + ",screenY=" + settings.popY + ",personalbar=no,scrollbars=no";
var newWin = window.open( "", "_blank", windowAttr );
newWin.doc = newWin.document;
return newWin;
}
If someone could explain the rational behind calling the popup function in new, I would really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,
Popup
是一个愚蠢的构造函数,无论如何都会返回一个对象,因此new
关键字毫无用处,因为this
不会在内部返回。对于这个 Popup 函数来说,这只是有效但无用的代码。
In this case
Popup
is a silly constructor that returns an object anyway so thenew
keyword is useless becausethis
is not returned internally.It's simply valid but useless code for this Popup function.
new
运算符创建Popup()
的新对象
。这样,如果您有多个不同的弹出窗口,您可以通过以下方式创建它们:
然后您就可以为它们提供自己的参数。
编辑感谢评论中的 pst
然而,这不是这里的情况。请注意,“构造函数”返回——因此“新对象”被默默丢弃,并返回 newWin 中包含的对象。
The
new
operator creates a newobject
ofPopup()
.This way, if you would have multiple different popups, you could create them by saying:
And then you would be able to give them their own parameters.
EDIT thanks to pst in the comments
This is not the situation here, however. Note that the "constructor" returns -- thus the "new object" is silently discarded and the object contained in newWin is returned.
Popup(类)的新实例被实例化并分配给变量 printWindow。
A new instance of Popup (class) is instantiated and assigned to the variable printWindow.