3

Possible Duplicate:
How does an anonymous function in JavaScript work?
What does this JavaScript snippet mean?

I use in my scripts:

(function(){
...[code]..
})()

I can't find documentation about this, i have see scripts when this form take args.

(function(){
...[code]..
})(arg1,arg2)

Somebody have a link or a good explanation about this javascript function?

Community
  • 1
  • 1
fpilee
  • 1,918
  • 2
  • 22
  • 38
  • That would be called a function - specifically, "Immediately Invoked Function Expression" – Ian Jan 28 '13 at 15:19
  • there's plenty about this one the web, but this one is pretty clear: http://markdalgleish.com/2011/03/self-executing-anonymous-functions/ – SDC Jan 28 '13 at 15:26

2 Answers2

1

this is just regular javascript.

you instanciate an anonymous function, and then call it with 2 arguments.

The confusing part I think is the on-the-fly aspect of the operation.

You could have done (at higher cost):

var hnd = function() {...};
hnd(arg1,arg2);
Sebas
  • 21,192
  • 9
  • 55
  • 109
1

It's known as a Self-Executing Anonymous Function.

Here is the first Google result, which gives a solid overview.

Bort
  • 7,398
  • 3
  • 33
  • 48