Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError for function(){}()

Please see this fiddle. For me, it's just a self-executing empty function:

function(){}()

Google Chrome 16.0.912.4 dev-m returns the error:

Uncaught SyntaxError: Unexpected token (

Why? This is especially strange because adding extraneous brackets will remove the error:

(function(){})()
like image 897
Randomblue Avatar asked Sep 08 '26 08:09

Randomblue


1 Answers

ExpressionStatement :

[lookahead ∉ {{, function}] Expression ;

Because a function () {}() is not a statement as defined in ES5.1

And a valid program has to be a statement.

Expression Statement.

however the following

!function () {}();

is a valid statement, so is using () and so is var ret = function () {}()

like image 113
Raynos Avatar answered Sep 10 '26 22:09

Raynos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!