JavaScript 中的链接构造函数
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该教育您的用户永远不要忘记
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.回答自己...
我一夜之间思考了我的问题...我想我发现了一些
更令人满意的解决方案:如果
this
是一个,则充当构造函数调用的实例。这个测试比检查更严格一些
是否不是全局对象,但仍然允许构造函数
链接,只要原型已正确链接即可。
这是我的本机构造函数的第一行(
SerialPort
是我的基类,围绕 QSerialDevice 构建:
有趣的是:与
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 aninstance 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 mybase class, built around QSerialDevice):
The funny thing about this is: unlike
isCalledAsConstructor()
, thistest can also be implemented in a JavaScript constuctor!