Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between these three form of self-invoking anonymous function? [duplicate]

Possible Duplicate:
Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript?

I'm reading the document below.

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#patternity

When I looked though these examples, self-invoking of an anonymous function had three forms.

The one was

(function() {
    //do something
})();

and another was

function() {
    //do something
}();

and the other was

(function() {
    //do something
}());

What's the difference between these three forms?

Thank you for your reading!

like image 701
synthresin Avatar asked Jan 12 '13 15:01

synthresin


1 Answers

The first and last are effectively identical. The differences are a matter of style.

The second is unsafe as (depending on where it is) it could be a function declaration instead of a function expression, and you can't immediately invoke a function declaration.

like image 82
Quentin Avatar answered Oct 23 '22 21:10

Quentin