开发者

Chrome - eval - function()

Why is Chrome throwing

SyntaxError: Unexpected token (

when i try to call:

开发者_如何学编程
eval("function(){alert('test')}")

?


Chrome is throwing the SyntaxError because you either need () around your function or you need to name it.

//This defines a as the function
eval("function a(){alert('foo')}");

//This returns the anonymous function
eval("(function(){alert('foo')})");

either should work properly.


Oddly, Safari 5.1 and Chrome 13.0.782.220 do require outer parens in this special case of anonymous functions. I have no idea why, as the expression is no more ambiguous or less useful without the parens than with. Firefox 6.0.2 is perfectly happy without the parens.

Does anyone know whether the Javascript language spec specifies these outer parens and whether they're necessary for cases other than anonymous functions?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜