Chrome - eval - function ()

Why cast Chrome

SyntaxError: Unexpected token ( 

when i try to call:

 eval("function(){alert('test')}") 

?

+7
source share
2 answers

Chrome throws a SyntaxError because you need () around your function, or you need to call it.

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

either should work correctly.

+10
source

Oddly enough, Safari 5.1 and Chrome 13.0.782.220 really require external parsers in this special case of anonymous functions. I have no idea why, because the expression is no more ambiguous or less useful without paren than with. Firefox 6.0.2 is great without partners.

Does anyone know if the JavaScript language specification specifies these external parades and are they needed for cases other than anonymous?

+1
source

All Articles