2

Possible Duplicate:
Self Executing function doesnt work?

In Javascript, why does

function() { console.log('hello'); }();

give an error, but

function() { console.log('hello'); }()

not?

Edit: some answers state that both give syntax errors in Firefox, but this does not seem to be the case in either Chrome or a Node.js REPL. Additionally,

(function() { console.log('hello'); }());

seems to work fine in Firefox.

Community
  • 1
  • 1
Dan Jaouen
  • 787
  • 1
  • 8
  • 21

5 Answers5

2

Actually both of those are SyntaxErrors.

You need to wrap parens around the anonymous function so it can be properly parsed.

(function(){alert('eyo')})();

This defines the function and immediately invokes it. It's not syntactically valid to define a function and invoke it without 'forming' the function by wrapping parens around it.

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

Fortunately, the SyntaxError “fix” is simple. The most widely accepted way to tell the parser to expect a function expression is just to wrap in in parens, because in JavaScript, parens can’t contain statements. At this point, when the parser encounters the function keyword, it knows to parse it as a function expression and not a function declaration.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
0
(function() { console.log('hello'); })();

You must invoke some function, in this case is "()"

No, any Syntax errors

NiLL
  • 13,645
  • 14
  • 46
  • 59
0

In Firefox, both lines generate the same error:

SyntaxError: function statement requires a name

To fix this, simply add parentheses:

(function() { console.log('hello'); })();

The error is probably raised because it indicates a syntax error:

function (value) {
    return value * value;
}

It's obvious that the function name is missing, and that no anonymous function was intended.

Therefore, anonymous functions always have to occur as expressions, e.g. to assign a variable:

var a = function() { };

Otherwise, you need the parentheses.

Yogu
  • 9,165
  • 5
  • 37
  • 58
0

an anonymous function that is followed by () is essentially you saying "immediately call this function" but your syntax is not correct because without the wrapping parents, the () doesn't attach to anything (there is an exception, that if you set your anonymous function equal to a variable, its ok, but you didn't do that... see comments).

so it must be like this: ( function(){} )()

think of it like this:

1. ( someExpressionReturnsFunction )(2)
2. is now: aFunction(2)
Kristian
  • 21,204
  • 19
  • 101
  • 176
0

As the others pointed out, there's a difference in assignment vs. an expression

Self-executed functions are functions that were turned into expressions:

// self-executed due to both being an expression, no function declaration.
!function(foo) { console.log('test', foo); }('first');
(function(foo) { console.log('test', foo); })('second');

// long-hand examples of normal usage without expression conversions.
var bar = function(foo) { console.log('test', foo); };
bar('third');

You can only pass through arguments with the least parens "()" afterwards if the thing before is either a callable already-declared function or an expression.

edit:// I added the negotiator example, because that will cause the following keyword being converted in an expression. expressions always return true or false.

Christoph Martens
  • 277
  • 1
  • 2
  • 10