(function(a){ alert(a); }(1)); (function (a) { alert(a); })(1); 

What is the difference between these functions?

Or from the example of Stefanov:

 var result = (function () { return 2 + 2; }()); (function () { alert('watch out!'); })(); 
  • Find a duplicate, anyone! - VladD 3:51 pm
  • 33173 , 391114 , 189242 ... - Mr. Black
  • four
    What have you pounced on? The man is asking. - user207618
  • Doofy, look carefully at my question and at the duplicates that you gave, I did not ask what this construction means, I asked what is the difference between the different recording options - ruslik

1 answer 1

In general, there is no difference.
Douglas Crockford found the record:

 (function () { //body }()); 

It is correct, and the notation:

 (function () { //body })(); 

Wrong and dismissively calls her "dog eggs" (dog balls).


Therefore, use the one that you like more (for example, I agree with Douglas, although the second option is not at all annoying).
For example, in Perl generally prescribed by the slogan that everything should have several ways of notation.
How is JS worse? :)

  • largescalejs.ru/module-pattern - by the way, here the author, too, in the example with jquery, implements the version of Douglas Crockford - ruslik
  • @ruslik, Douglas is a respected man, his opinion has weight. Plus, in my opinion, the construction }()); easier to type than })(); . - user207618