0
(function (foo) { alert(10); } ());

(function (foo) { alert(10); } )();

Both are anonymously invoked function, but there is a difference in the code structure. I am trying to understand the difference. Thanks in advance!

Andy
  • 39,764
  • 8
  • 53
  • 80
meme
  • 33
  • 1

1 Answers1

1

There is no difference between them at all. The outer parens are just there to force the parser to treat function as the beginning of an expression rather than the beginning of a declaration; whether you put the () calling the resulting function inside or outside of them doesn't matter. You can even not use the outer parens at all:

+function(foo) { alert(10); }();

The + also makes the parser expect an expression rather than a declaration.

More about this aspect of parsing in this answer.

Community
  • 1
  • 1
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639