0

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?

Alloys
  • 143
  • 1
  • 11
  • 1
    possible duplicate of [Location of parenthesis for auto-executing anonymous JavaScript functions?](http://stackoverflow.com/questions/3384504/location-of-parenthesis-for-auto-executing-anonymous-javascript-functions) – Grundy Jun 14 '15 at 10:09
  • and this - http://stackoverflow.com/questions/8774425/vs-in-javascript-closures – Abhishek Jain Jun 14 '15 at 10:16

1 Answers1

2
(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.

Stefan Dimov
  • 581
  • 3
  • 11