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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With