0

I'm relatively new to JavaScript, I'm currently studying it and I came to this kind of code with self-executing functions.

Here is the first one:

(function(message){
    var myMessage = message;
    alert(myMessage);
})("hello");

Here is the second:

(function(message){
    var myMessage = message;
    alert(myMessage);
}("hello"));

Both of them work correctly but I'm just wondering what's the difference between the two and when should I use one over the other.

Sir/Ma'am your answers would be of great help. Thank you++

Randel Ramirez
  • 3,671
  • 20
  • 49
  • 63

1 Answers1

1

Both are valid, though there is a slight semantic difference between the two, which I explained in the IIFE's tag wiki as follows:

Apart from there being a minor semantic difference between the two (the first evaluates to (returnvalue of IIFE), whereas the second evaluates to (defined function)<=(call)) they are both equally valid, though the renowned JavaScript expert Douglas Crockford considers the second notation as being "wrong" and "illogical".

In other words, both are correct, but because both render the function definition into an expression, rather than a statement, though the first version groups the definition of the function, to then invoke that function, whereas the second version defines the function, invokes it, and then groups its return value...

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • `"(" callExpression ")"` vs `callExpression` have absolutely no semantic difference and both evaluate to `callExpression` – Esailija Aug 04 '13 at 14:21
  • The difference is in what the grouping operators evaluate to: `(function(){})();` == `(functionExpression)()` whereas `(function(){}());` == `(returnValue)` – Elias Van Ootegem Aug 04 '13 at 14:26
  • `(function(){})()` -> `"(" FunctionExpression ")" Arguments` -> `FunctionExpression Arguments` -> `CallExpression` and `(function(){}())` -> `"(" FunctionExpression Arguments ")"` -> `"(" CallExpression ")"` -> `CallExpression`. So when you get at the AST you will not be able tell what the original was and thus may not generate code that defines function and calls it vs returning value or whatever you are claiming – Esailija Aug 04 '13 at 14:33
  • I agree with @Esailija, it does seem a [distinction without a difference](http://en.wiktionary.org/wiki/distinction_without_a_difference). – Jared Farrish Aug 04 '13 at 14:43
  • @Esailija, I'm not saying there's a difference in how the code will work. The statements will both evaluate to exactly the same thing, because `(func)()` === `return value` === `(return value)`. Just saying you're grouping something, which is why DC would consider the first of the OP's snippets as illogical and wrong – Elias Van Ootegem Aug 04 '13 at 15:29
  • @EliasVanOotegem What was claimed in the wiki is that the semantics differ - but according to the grammer specified for Javascript, it doesn't. The wiki even claims difference in evaluation - even though they are already the same at the grammer level... :( – Esailija Aug 04 '13 at 15:39