JQuery 中的 new 运算符意味着

发布于 2024-11-15 15:34:47 字数 689 浏览 2 评论 0原文

我在互联网上发现了这段代码。它是关于实现打印功能的插件。我只是想知道调用该函数时 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妞丶爷亲个 2024-11-22 15:34:47
printWindow = new Popup();
// is the same as
// create a new `this` scope for the Popup function from the prototype.
var temp_this = Object.create(Popup.prototype);
// call the function with the `this` context and store the result.
var o = Popup.call(temp_this);
// if the result is an object then assign it to the variable
// otherwise assign the `this` value to the variable.
printWindow = typeof o === "object" ? o : temp_this;
// It actually does more. Go read the ES5 spec.

在这种情况下,Popup 是一个愚蠢的构造函数,无论如何都会返回一个对象,因此 new 关键字毫无用处,因为 this 不会在内部返回。

对于这个 Popup 函数来说,这只是有效但无用的代码。

printWindow = new Popup();
// is the same as
// create a new `this` scope for the Popup function from the prototype.
var temp_this = Object.create(Popup.prototype);
// call the function with the `this` context and store the result.
var o = Popup.call(temp_this);
// if the result is an object then assign it to the variable
// otherwise assign the `this` value to the variable.
printWindow = typeof o === "object" ? o : temp_this;
// It actually does more. Go read the ES5 spec.

In this case Popup is a silly constructor that returns an object anyway so the new keyword is useless because this is not returned internally.

It's simply valid but useless code for this Popup function.

你列表最软的妹 2024-11-22 15:34:47

new 运算符创建 Popup() 的新对象
这样,如果您有多个不同的弹出窗口,您可以通过以下方式创建它们:

popup1 = new Popup();
popup2 = new Popup();

然后您就可以为它们提供自己的参数。

编辑感谢评论中的 pst

然而,这不是这里的情况。请注意,“构造函数”返回——因此“新对象”被默默丢弃,并返回 newWin 中包含的对象。

The new operator creates a new object of Popup().
This way, if you would have multiple different popups, you could create them by saying:

popup1 = new Popup();
popup2 = new Popup();

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.

巷雨优美回忆 2024-11-22 15:34:47

Popup(类)的新实例被实例化并分配给变量 printWindow。

A new instance of Popup (class) is instantiated and assigned to the variable printWindow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文