JSLint Messages – Move the invocation into the parens that contain the function

With this message we move deep into the heart of JavaScript closures. Without wanting to engage into a discussion about what they or their relative merits are, we can examine how this message might arise. Take the following code:

var a = 0;
(function(a) {

The parentheses around the function are necessary, because when omitted, the compiler assumes it is a Function Statement (see links below).

When referring to ‘Invocation’ we actually mean the bit of code (a) at the very end of the block of code. Although the code itself will execute perfectly, JSLint prefers that everything is more self-contained, so insists on the following convention instead:

var a = 0;
(function(a) {

This is almost identical, except the invocation (a) now appears within the parentheses that wrap the function.

The most convincing for me is that the initial parentheses isolates the block of code that is being invoked by the second set of parentheses. In addition to this, JSLint has been designed specifically to eliminate conventions and behaviours that have been proven to lead to bugs and issues, so it is possible that sometime in the past the first convention has yielded a bug or some sort of issue.

Some Links

For more information on closures, refer to Closures in JavaScript by James Padolsey.

The above function code follows a convention more commonly known as the JavaScript Module Pattern

To demystify the mire of Function Declarations, Function Expressions and Function Statements, refer the article Named function expressions demystified by Juriy “kangax” Zaytsev.

Also thanks to StackOverflow for helping me answer this: Question –  Solution for JSLint Errors

A Guide To JSLint Messages

This article is one of a series on the error and warning messages produced by JSLint.