Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript anonymous function syntax variations [duplicate]

Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?
Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript?

I have seen the javascript anonymous functions written two different ways:

(function(){

})();

and

(function(){

}());

I have always thought the top one to be correct, and had never encountered the bottom one until now. I think this probably makes no difference at all but I thought I shoud be sure. Is there any difference between the two or are both ways equivalent?

like image 340
CRice Avatar asked Sep 16 '26 04:09

CRice


1 Answers

They're equivalent.

The opening parenthesis is the important bit - it's the one that helps the parser figure out that what's coming is a function expression rather than a function declaration.

See http://kangax.github.com/nfe/ for an explanation of the difference.

like image 132
Alnitak Avatar answered Sep 18 '26 19:09

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!