2011-04-04 17 views
0

我不知道如何可以在其它JS引擎(至少是IE6 +和Chrome瀏覽器)模擬犀牛(和SpiderMonkey的?)的StopIteration。以下條件必須成立。模擬犀牛的StopIteration其它JS引擎

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

更新2011-04-05

我看到的StopIteration的實施Mochikit,然後我檢查了NamedError code。我在前面的代碼中實現了以下代碼庫。然而,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 

回答

0

大聲笑,我簡直不敢相信我找到了什麼。我可以只使用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 = Object; alert(SI === SI); alert(SI instanceof SI);

不過,我還沒有在Chrome測試這一點,但我認爲它將工作。

更新2011/5/23

測試在Chrome和它按預期工作。

0

...但很多事情是instanceof Object
你的榜樣:

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 
0

如何:

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

這符合原來的兩個條件,並修復了兩個斯科特的例子,讓他們返回false(new Error() instanceof SInew TypeError() instanceof SI)。其他三個仍然返回true。此外SI instanceof Function返回true,而StopIteration instanceof Function將返回false。

如果一個對象可以給予一個[[HasInstance]]內部方法(這是需要使用它的上instanceof的右側),而不被一個Function,那麼它可能是能夠固定這些其他問題。我不知道有什麼辦法,雖然這樣做。