8

Possible Duplicates:
What does this JavaScript snippet mean?
Location of parenthesis for auto-executing anonymous JavaScript functions?

(function(){

    //something here...

})() <--//This part right here.

What exactly is this )()?
What if I change it to this ())?

(function(){

    //something here...

}()) <--//Like this
Community
  • 1
  • 1
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247

8 Answers8

5

They are the same.

There has to be a parenthesis either around the function definition or around the function call to make it valid Javascript syntax, but it doesn't matter which you use.

To demonstrate what it does, using a named function it would be:

function something() {}

// parenthesis around the function reference:
(something)();

// parenthesis around the function call:
(something());
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
5

This declares an anonymous function and calls it immediately.

The upside of doing this is that the variables the function uses internally are not added to the current scope, and you are also not adding a function name to the current scope either.

It is important to note that the parentheses surrounding the function declaration are not arbitrary. If you remove these, you will get an error.

Finally, you can actually pass arguments to the anonymous function using the extra parentheses, as in

(function (arg) {
   //do something with arg
})(1);

See http://jsfiddle.net/eb4d4/

Yahel
  • 37,023
  • 22
  • 103
  • 153
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • -1. Unless I'm overlooking something, the second example is perfectly valid syntax. – Yahel Jul 21 '11 at 23:00
  • 2
    why you think his second example is invalid? – jAndy Jul 21 '11 at 23:02
  • FYI, every browser from ie6 to the latest chrome and firefox accept the second version, and I've seen it recommended several times over the first. You only need to make sure your entire function call is within parenthesis; eg (function(){ console.log('test'); }()). From [Crockford's website](http://javascript.crockford.com/code.html): "When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself." – Lepidosteus Jul 21 '11 at 23:02
3

It's an anonymous function that gets called immediately the () calls the function and there are ( and ) wrapping the whole thing.

( // arbitrary wrapping
(function() { // begin anon function

}) // end anon function
() // call the anon function
) // end arbitrary wrapping
JohnKlehm
  • 2,368
  • 15
  • 9
2

The first one just wraps the function in ( ) so that it can call the function immediately with the ()

(function(){
    alert('Hi');
})();

Alerts Hi, while

function(){
    alert('Hi');
}

Doesn't do anything since your function is never executed.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • 2
    1600 points this week? great going paul :) – naveen Jul 21 '11 at 22:52
  • @yahelc, what do you know. I just tested it and you're right. I thought I had tried it that way before once and it didn't work and I've always assumed that was the case since... I must've made some other error back then, haha. Edited my post :) – Paul Jul 21 '11 at 23:00
  • @yahelc actually I think what I tried was without any brackets, like `function(){ //Code }();` – Paul Jul 21 '11 at 23:01
  • @PaulPro Removed the downvote. Thanks for fixing :) – Yahel Jul 21 '11 at 23:02
1

Its an immediately invoked anonymous function. ()) would not work because you need () around the function before you can call it with ().

Sorta equivalent to:

function a(){}
a();
Yahel
  • 37,023
  • 22
  • 103
  • 153
John Stimac
  • 5,335
  • 8
  • 40
  • 60
  • 2
    It's not self-calling, it's an anonymous function that is called immediately after it's created. If you want to make self-calling anonymous functions read this blog post: http://amix.dk/blog/post/19239 – Paul Jul 21 '11 at 22:50
1

That's simply an anonymous function. The () parens call the function immediately, instead of waiting for it to be called elsewhere.

GarlicFries
  • 8,095
  • 5
  • 36
  • 53
0

This is declaring an anonymous function and then immediately executing it. This is common for creating scoped variables.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
0

In JavaScript, function definition is a literal - which means, it's an expression with the value of the Function object.

If you put () right after it, you effectively call the function right after defining it.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277