Self Executing Anonymous Function Syntax

180 views Asked by At

I often write my JS self executing anonymous functions like this

(function(){})()

but the other day I saw this, in somebody's code

(function(){}())  

what's the difference, and is one recommended over the other?

1

There are 1 answers

0
Stefan Dimov On
(function(){}());

I recommended this one, because it makes more sense.

You have your function function(){} then you append the () to execute it, then you wrap the whole thing in () to specify that it's an expression. That is done so the js interpreter will not define it as a function declarations, but as a function expression.

But it doesn't matter, it will execute properly, so it's a personal taste problem.