0

Look at the placement of the parenthesis, is that any different?

( func )( )

(function(){

})();

and ( func( ) )

(function(){

}());
Kent Liau
  • 915
  • 15
  • 26
  • It does the same thing, but the second one is more semantically correct IMO. – elclanrs May 13 '14 at 07:21
  • Same thing only one is wrapped in parenthesis which don't do anything... – hitautodestruct May 13 '14 at 07:25
  • get your dog balls in! – Sten Muchow May 13 '14 at 07:26
  • 1
    Also: [Location of parenthesis for auto-executing anonymous JavaScript functions?](https://stackoverflow.com/questions/3384504/location-of-parenthesis-for-auto-executing-anonymous-javascript-functions) and [Is there a difference between (function() {…}()); and (function() {…})();?](http://stackoverflow.com/q/3783007/218196) – Felix Kling May 13 '14 at 07:26

1 Answers1

2

Technically the first defines an anonymous function, then calls it, the second defines an anonymous function which calls itself as it's defined. Realistically, they are identical.

Matt Fellows
  • 6,512
  • 4
  • 35
  • 57
  • What's the difference between "then calls it" and "calls itself"? How can something "call itself"? – Bergi May 13 '14 at 07:45
  • 1
    It's to do with returns and calls. The first set of code, creates a function which is returned to the caller, then the caller executes it. The second returns the result of the executed function (i.e. the function has self executed). In a much more complicated scenario I guess this could have different results, but I can't actually think of such a scenario. – Matt Fellows May 13 '14 at 08:00
  • 1
    No, the function is not "returned" to anybody. The function expression is *evaluated* to a function, as a part of a call expression - in both cases. There is *no* scenario in which they have a different result, except when the syntactic elements are parts of expressions that mean something completely different (when the grouping brackets are no grouping operator). – Bergi May 13 '14 at 09:41