为其他JS引擎模拟Rhino的StopIteration

发布于 2024-10-29 05:23:50 字数 945 浏览 7 评论 0 原文

我想知道如何在其他 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

I wonder how can I simulate StopIteration of rhino (and spidermonkey?) in other JS engines (at least those of IE6+ and Chrome). The following conditions must hold true.

StopIteration === StopIteration; // true
StopIteration instanceof StopIteration // true

Update 2011-04-05:

I saw an implementation of StopIteration in Mochikit, then I checked the NamedError code. I implemented the following code base on the earlier codes. However, the instanceof test still failed.

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 技术交流群。

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

发布评论

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

评论(3

北斗星光 2024-11-05 05:23:50

哈哈,我简直不敢相信我所发现的。我可以只使用JS的Object

Object === Object // true
Object instanceof Object // true

因此,在 StopIteration 中应用,

var SI = Object// using another object just to test my condition
SI === SI // true
SI instanceof SI // true

您可以在浏览器的网址栏上尝试:
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.

Object === Object // true
Object instanceof Object // true

So, applying in StopIteration

var SI = Object// using another object just to test my condition
SI === SI // true
SI instanceof SI // true

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.

泅人 2024-11-05 05:23:50

...但是很多东西都是对象的实例
以你的例子:

 var SI = Object
 undefined
  SI === SI  //true

  SI instanceof SI      //true

  Object instanceof SI     //true

  Error instanceof SI     //true

  new Error() instanceof SI     //true

  TypeError instanceof SI     //true

  new TypeError() instanceof SI     //true

...but lots of things are instanceof Object.
With your example:

 var SI = Object
 undefined
  SI === SI  //true

  SI instanceof SI      //true

  Object instanceof SI     //true

  Error instanceof SI     //true

  new Error() instanceof SI     //true

  TypeError instanceof SI     //true

  new TypeError() instanceof SI     //true
忆梦 2024-11-05 05:23:50

怎么样:

var SI = function() {};
SI.prototype = Function.prototype;

这满足了原来的两个条件,并修复了 Scott 的两个示例,使它们返回 false(new Error() instanceof SInew TypeError() instanceof SI)。但其他三个仍然返回 true。此外,SI instanceof Function 返回 true,而 StopIteration instanceof Function 将返回 false。

如果一个对象可以被赋予一个 [[HasInstance]] 内部方法(需要在 instanceof 的右侧使用它),而不是一个 Function ,那么就有可能解决这些其他问题。但我不知道有什么办法可以做到这一点。

How about:

var SI = function() {};
SI.prototype = Function.prototype;

This meets the original two conditions, and fixes two of Scott's examples so they return false (new Error() instanceof SI and new TypeError() instanceof SI). The other three still return true though. Also SI instanceof Function returns true, while StopIteration 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 of instanceof), without being a Function, then it might be possible to fix these other issues. I don't know any way to do that though.

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