JavaScript 中的链接构造函数

发布于 2024-09-28 11:03:36 字数 1148 浏览 6 评论 0原文

我正在尝试在 JavaScript 中实现某种类层次结构。我 我想我理解了原型链,但我仍然需要理清 构造函数链接。遵循 David Flanagan 的权威指南, 我写到

function DerivedClass()
{
    BaseClass.apply(this, arguments);  // chain constructors
    // do some initializations specific to DerivedClass...
}

var foo = new DerivedClass();

BaseClass() 是我用 C++ 编写的本机函数(我是 使用 QtScript)。我的问题是,然后调用 BaseClass() 作为函数,而不是构造函数。

我可以编写 BaseClass() 使其始终充当构造函数,但是它 被称为。但我担心有一天我的用户可能会忘记new 并只是写

var bar = BaseClass();

在这种情况下,我希望 BaseClass() 做更多的事情 比初始化全局对象更明智。例如:

if (!context->isCalledAsConstructor()) fail_gracefully();

但是构造函数链接失败了!

有没有办法可以链接构造函数并使用 BaseClass() 实际上被称为构造函数?或者我应该教育我的用户 永远不会忘记new?现在我很想替换上面的测试 by:

if (context->thisObject().strictlyEquals(engine->globalObject()))
    fail_gracefully();

但我想知道是否有更干净的方法来处理这个问题。

谢谢!

I'm trying to implement some kind of class hierarchy in JavaScript. I
think I understood the prototype chain, but I still have to sort out the
constructor-chaining. Following David Flanagan's Definitive Guide,
I wrote

function DerivedClass()
{
    BaseClass.apply(this, arguments);  // chain constructors
    // do some initializations specific to DerivedClass...
}

var foo = new DerivedClass();

where BaseClass() is a native function of mine written in C++ (I'm
using QtScript). My problem is that then, BaseClass() is called
as a function, not as a constructor.

I could code BaseClass() to always behave as a constructor, however it
is called. But I am afraid some day one of my users might forget new
and just write

var bar = BaseClass();

In such a situation, I would like BaseClass() to do something more
sensible than initializing the global object. For example:

if (!context->isCalledAsConstructor()) fail_gracefully();

But then the constructor chaining fails!

Is there a way I can chain the constructors and have BaseClass()
actually be called as a constructor? Or should I just educate my users
to never forget new? Right now I'm tempted to replace the test above
by:

if (context->thisObject().strictlyEquals(engine->globalObject()))
    fail_gracefully();

but I wonder if there is a cleaner way to handle this.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

花想c 2024-10-05 11:03:36

您应该教育您的用户永远不要忘记new

JavaScript 中的所有“构造函数”毕竟只是函数,因此您无法防止构造函数被作为函数调用。

在不使用 new 的情况下尝试创建新对象是不正确的 JavaScript。仅仅因为没有 Java 中的“编译时警告”,并没有什么不同。

You should educate your users to never forget new.

All "constructors" in JavaScript are just functions after all, so you can't protect against a constructor being called as a function.

It's incorrect JavaScript to try and create a new object without using new. Just because there isn't a "compile time warning" as there is in Java, doesn't make it any different.

清风疏影 2024-10-05 11:03:36

回答自己...

我一夜之间思考了我的问题...我想我发现了一些
更令人满意的解决方案:如果 this 是一个,则充当构造函数
调用的实例。这个测试比检查更严格一些
是否不是全局对象,但仍然允许构造函数
链接,只要原型已正确链接即可。

这是我的本机构造函数的第一行(SerialPort 是我的
基类,围绕 QSerialDevice 构建:

/*
 * Should we behave as a constructor?
 *
 * We could use context->isCalledAsConstructor() to decide. However,
 * we may want to subclass SerialPort in JavaScript and chain the
 * constructors:
 *
 *      function DerivedClass()
 *      {
 *          SerialPort.apply(this, arguments);
 *          // do some more initializations...
 *      }
 *
 * This would fail if we decided on the basis of
 * context->isCalledAsConstructor(). The test below is somewhat less
 * strict. It allows constructor chaining provided the prototypes
 * have been properly chained.
 */
bool behave_as_constructor =
    context->thisObject().instanceOf(context->callee());

有趣的是:与 isCalledAsConstructor(),这个
测试也可以在 JavaScript 构造函数中实现!

Answering myself...

I thought about my problem overnight... and I think I found a somewhat
more satisfying solution: behave as a constructor if this is an
instance of the calle. This test is somewhat stricter than checking
whether it's not the global object, but it still allows constructor
chaining, as long as the prototypes have been properly chained.

Here are the first lines of my native constructor (SerialPort is my
base class, built around QSerialDevice):

/*
 * Should we behave as a constructor?
 *
 * We could use context->isCalledAsConstructor() to decide. However,
 * we may want to subclass SerialPort in JavaScript and chain the
 * constructors:
 *
 *      function DerivedClass()
 *      {
 *          SerialPort.apply(this, arguments);
 *          // do some more initializations...
 *      }
 *
 * This would fail if we decided on the basis of
 * context->isCalledAsConstructor(). The test below is somewhat less
 * strict. It allows constructor chaining provided the prototypes
 * have been properly chained.
 */
bool behave_as_constructor =
    context->thisObject().instanceOf(context->callee());

The funny thing about this is: unlike isCalledAsConstructor(), this
test can also be implemented in a JavaScript constuctor!

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