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

2

9

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!

Detest answered 12/1, 2013 at 15:13 Comment(1)
The second one is a syntax error if it stands alone.Criner
T
6

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.

Thiol answered 12/1, 2013 at 15:14 Comment(0)
A
1

The proceeding function is not valid syntax:

function() {
    //do something
}();
Aerometry answered 12/1, 2013 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.