为其他JS引擎模拟Rhino的StopIteration
我想知道如何在其他 JS 引擎(至少是 IE6+ 和 Chrome 引擎)中模拟 rhino(和 Spidermonkey?)的 StopIteration
。以下条件必须成立。
StopIteration === StopIteration; // true
StopIteration instanceof StopIteration // true
2011-04-05 更新:
我看到了 StopIteration,然后我检查了 命名错误代码。我在早期代码的基础上实现了以下代码。但是,instanceof
测试仍然失败。
NamedError = function (name) {
this.message = name;
this.name = name;
};
NamedError.prototype = new Error;
NamedError.prototype.constructor = NamedError;
StpIter = new NamedError("StpIter") // just to see if I can simulate StpIter to be like Mozilla's StopIteration.
print(StpIter === StpIter) // true
print(StpIter instanceof StpIter) // false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哈哈,我简直不敢相信我所发现的。我可以只使用JS的
Object
。因此,在 StopIteration 中应用,
您可以在浏览器的网址栏上尝试:
javascript: SI = 对象;警报(SI === SI); alert(SI instanceof SI);
不过,我还没有在 Chrome 上测试过这个,尽管我认为它会起作用。
更新 5/23/2011
在 Chrome 中进行测试,效果符合预期。
LOL, I could not believe what I've found. I can just use the JS's
Object
.So, applying in StopIteration
you may try it on your browser's url bar:
javascript: SI = Object; alert(SI === SI); alert(SI instanceof SI);
However, I haven't tested this on Chrome, though I think it will work.
Update 5/23/2011
Tested in Chrome and it works as expected.
...但是很多东西都是
对象的实例
。以你的例子:
...but lots of things are
instanceof Object
.With your example:
怎么样:
这满足了原来的两个条件,并修复了 Scott 的两个示例,使它们返回 false(
new Error() instanceof SI
和new TypeError() instanceof SI
)。但其他三个仍然返回 true。此外,SI instanceof Function
返回 true,而StopIteration instanceof Function
将返回 false。如果一个对象可以被赋予一个
[[HasInstance]]
内部方法(需要在instanceof
的右侧使用它),而不是一个Function
,那么就有可能解决这些其他问题。但我不知道有什么办法可以做到这一点。How about:
This meets the original two conditions, and fixes two of Scott's examples so they return false (
new Error() instanceof SI
andnew TypeError() instanceof SI
). The other three still return true though. AlsoSI instanceof Function
returns true, whileStopIteration instanceof Function
would return false.If an object could be given a
[[HasInstance]]
internal method (which is required to use it on the right side ofinstanceof
), without being aFunction
, then it might be possible to fix these other issues. I don't know any way to do that though.