Immediate function invocation syntax

JavascriptSyntaxJslintIife

Javascript Problem Overview


There is a JSLint option, one of The Good Parts in fact, that "[requires] parens around immediate invocations," meaning that the construction

(function () {
  
  // ...
  
})();

would instead need to be written as

(function () {
  
  // ...
  
}());

My question is this -- can anyone explain why this second form might be considered better? Is it more resilient? Less error-prone? What advantage does it have over the first form?


Since asking this question, I have come to understand the importance of having a clear visual distinction between function values and the values of functions. Consider the case where the result of immediate invocation is the right-hand side of an assignment expression:

var someVar = (function () {
  
  // ...
  
}());

Though the outermost parentheses are syntactically unnecessary, the opening parenthesis gives an up-front indication that the value being assigned is not the function itself but rather the result of the function being invoked.

This is similar to Crockford's advice regarding capitalization of constructor functions -- it is meant to serve as a visual cue to anyone looking at the source code.

Javascript Solutions


Solution 1 - Javascript

From Douglass Crockford's style convention guide: (search for "invoked immediately")

When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself.

So, basically, he feels it makes more clear the distinction between function values, and the values of functions. So, it's an stylistic matter, not really a substantive difference in the code itself.

updated reference, old PPT no longer exists

Solution 2 - Javascript

Immediately Called Anonymous Functions get wrapped it in parens because:

  1. They are function expressions and leaving parens out would cause it to be interpreted as a function declaration which is a syntax error.

  2. Function expressions cannot start with the word function.

  3. When assigning the function expression to a variable, the function itself is not returned, the return value of the function is returned, hence the parens evaluate what's inside them and produce a value. when the function is executed, and the trailing parens ..}() cause the function to execute immediately.

Solution 3 - Javascript

Or, use:

void function () {
...
} ()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBobby EickhoffView Question on Stackoverflow
Solution 1 - JavascriptcgpView Answer on Stackoverflow
Solution 2 - JavascriptDathanView Answer on Stackoverflow
Solution 3 - JavascriptAgamemnusView Answer on Stackoverflow