1

Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?

Question is a duplicate of Location of parenthesis for auto-executing anonymous JavaScript functions? and What do parentheses surrounding a object/function/class declaration mean?

Just curious really, what are the purposes of the brackets in this code:

(function() {})();

This looks like I could just as easily write:

var x=function(){};
(x)();

With jQuery plugins we would do something like...

(function($) {})(jQuery);

What's the deal with the brackets?

Community
  • 1
  • 1
Incognito
  • 20,537
  • 15
  • 80
  • 120
  • 1
    This is probably a duplicate. – Gumbo Oct 06 '10 at 18:54
  • 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) and of [Is there a difference between (function() {…}()); and (function() {…})(); ?](http://stackoverflow.com/questions/3783007/is-there-a-difference-between-function-and-function) – Peter Ajtai Oct 06 '10 at 18:56
  • 1
    @Gumbo: yes, found [a duplicate](http://stackoverflow.com/questions/440739/what-do-parentheses-surrounding-a-javascript-object-function-class-declaration-me) (already a duplicate as well). – palswim Oct 06 '10 at 18:56
  • 1
    Thanks for pointing it out. Please close this. In Canada we don't commonly use the term parenthesis to refer to any of these charecters: () {} [], which get called brackets (eg, we call it BEDMAS instead of PEDMAS) – Incognito Oct 06 '10 at 18:59
  • @user257493 - [ **Wikipedia expounds on brackets.** ](http://en.wikipedia.org/wiki/Bracket) – Peter Ajtai Oct 06 '10 at 19:02

2 Answers2

2

With the parentheses (surrounding the function), you don't have to declare a name, littering the namespace. And, they serve to alert readers of your code to the fact that you're using a self-invoking function.

The second set of parentheses actually invoke/call the (anonymous) function you just created. Since Javascript functions are actually just variables (or "first-class objects" in CS-speak), you've just created a variable (in the first set of parentheses), which you call using the second set.

Here's an example:

function callFunc(f) {
    return f("test");
}

callFunc(alert);

In the example, f actually references the function alert, which you call in the function code with the parentheses.

palswim
  • 11,856
  • 6
  • 53
  • 77
1

In case of the jQuery example you define an anonymous function that takes a parameter called $ and then pass the jQuery object to it. It will stay inside that scope and not conflict with other frameworks that have $ globally defined.

Other than that, personally it looks cleaner to me.

Daniel Sloof
  • 12,568
  • 14
  • 72
  • 106
  • I'm not really concerned with what the jQuery plugin code does as much as I am the purposes of the brackets. – Incognito Oct 06 '10 at 18:57
  • That's more the reason for enclosing the code in a function, and not so much a reason for the parentheses themselves. – palswim Oct 06 '10 at 19:07