4

Only functions expressions can be immediately invoked:

(function () {
    var x = "Hello!!";      // I will invoke myself
})();

But not function declarations? Is this because function declarations are hoisted and already execute immediately?

EDIT: Resources I'm referencing

http://benalman.com/news/2010/11/immediately-invoked-function-expression/

http://markdalgleish.com/presentations/gettingclosure/

amoeboar
  • 355
  • 1
  • 4
  • 22
  • function x(){} is ~the same as var x=function(){}, and an explicit var "returns" void instead of the assignment. that's why you can't say alert(var x=1), but you can say alert(x=1); same for functions. – dandavis Oct 13 '14 at 08:02

3 Answers3

3

Source:

"...While parens placed after an expression indicate that the expression is a function to be invoked, parens placed after a statement are totally separate from the preceding statment, and are simply a grouping operator (used as a means to control precedence of evaluation)."

// While this function declaration is now syntactically valid, it's still
// a statement, and the following set of parens is invalid because the
// grouping operator needs to contain an expression.
function foo(){ /* code */ }(); // SyntaxError: Unexpected token )

// Now, if you put an expression in the parens, no exception is thrown...
// but the function isn't executed either, because this:

function foo(){ /* code */ }( 1 );

// Is really just equivalent to this, a function declaration followed by a
// completely unrelated expression:

function foo(){ /* code */ }

( 1 );

Therefore, you need to write the function as

(function doSomething() {})();

as this tells the parser to evaluate it as a function expression as opposed to a function declaration. And all you're doing then is immediately invoking the expression.

aryeh
  • 198
  • 3
  • 7
2

To clear the confusion

What is Function declaration

// this is function declaration
function foo(){
  // code here
}

OR

//this is ok, but without name, how would you refer and use it
function (){
  // code here
}

to call it immediately you do this

function foo(){
  // code here
}()

What is Function expression

// this is a function expression
var a = function foo(){
 // code here
};

or

var a = function (){
  // code here
};

in the second case you have created a anonymous function.you still have a reference to the function through the variable a.so you could do a().

invoking function expression

var a = (function (){
  // code here
}());

the variable a is stored with the result of the function(if you return from the function) and loses the reference to the function.

In both the cases you can immediately invoke a function but the outcome differs as pointed above.

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
1

Not sure what you mean exactly - if you run a function declaration in the way you have shown it will still execute immediately

(function declaredFn(){
  document.getElementById('result').innerHTML='executed';
}());
<div id="result"></div>
codebox
  • 19,927
  • 9
  • 63
  • 81
  • This is not a function declaration, if you'd [call `declaredFn` later](http://jsfiddle.net/faxbLfot/), it would cause a reference error. – Teemu Oct 13 '14 at 07:25
  • Sorry, just to clarify, I've learned that only function expressions can be immediately invoked, but you cannot immediately invoke function declarations. I was wondering why this is. – amoeboar Oct 13 '14 at 07:40
  • Enclosing `function () {}` within parens makes it expression. – Teemu Oct 13 '14 at 09:00