Firefox 3.0.2 の window.eval の挙動変更
Firefox を 3.0.2 に上げたら Vimperator plugin の ldrize_cooperation.js が動かなくなったので、ちょいちょいいじってみると window.eval のあたりでこけている模様。
挙動を調べてみると、どうやら 3.0.2 以降(3.0.3 でも)では eval() の第 2 引数が効かなくなっています。
var a = "global a"; var context = { a: "context a" }; window.eval("alert(a)"); // "global a" window.eval("alert(a)", context); // "context a" だったけど、3.0.2 では "global a" // いろいろ試してみるけど、だめ。 eval("alert(a)", context); // "global a" context.eval("alert(a)"); // Error! context.eval is not a function eval.call(context, "alert(a)"); // Error! function eval must be called directly, and not by way of a function of another name
で、結局 context の下で評価できたのは with だけでした。
with (context) { eval("alert(a)"); // "context a" }
MDC を覗いてみると、eval() の第二引数は次のように書かれています。
object(Non-standard)
eval - MDC
An optional argument; if specified, the evaluation is restricted to the context of the specified object.
下の方も読んでみると、Rhino や Safari じゃ使えないぜ、互換性考えるんなら with 使えよ!って書いてありますね。
Firefox もより標準に合わせて使えなくしたということでしょうか。