31

I have started using JSLint. I checked my code and I am getting this errors:

Problem at line 92 character 7: Move the invocation into the parens that contain the function.
})();

Problem at line 92 character 7: Wrap the entire immediate function invocation in parens.
})();

How To Fix this errors?

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123

2 Answers2

59

I believe this means you should move the function calling parens inside the wrapping parens

(function() { /* code */ })() 

The two last parens that execute the function are the problem. This is how jslint wants it to look like:

(function() { /* code */ }()) 
Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
  • 10
    That's strange. The convention is usually to wrap everything before last invocation parens - `(function(){})()`. – kangax Sep 20 '09 at 19:38
  • 5
    Well, since when did jslint make complete sense? ;) Personally I just ignore the rules which don't make sense, such as this one. – Jani Hartikainen Sep 21 '09 at 05:38
  • 14
    "since when did jslint make complete sense?", I know that was made in jest, but every rule that crockford put in there has some lengthy justification. You could say that many of the rules aren't very important, but the rules, at least, have a solid justification. – Mark Rogers Nov 30 '09 at 23:06
35

I found a good explanation here: http://james.padolsey.com/javascript/closures-in-javascript/

The first set of parentheses (around "function(){}") isn't required but is used to make it obvious that the function is immediately invoked, thus making it obvious that the expression does not necessarily return that function; but instead the return value of that function

Rob
  • 909
  • 1
  • 8
  • 14
  • 1
    This makes perfect sense, especially when it's a big function. It makes it very obvious what's going on - something that helps when reading the code another person wrote, or your own code after 3 months of not looking at it. Code, after all, is read much more often than it is written. – Tim Kersten Apr 20 '10 at 08:55
  • 3
    Actually, it is required if the 'function' token is the first thing on the line. You get a syntax error otherwise, because it's treated as a function declaration, instead of an expression. – scribu Feb 16 '11 at 12:27
  • 4
    It's been more than two years since this was posted, but I feel a should point out that, if you follow the link, you'll find that the code being described in the above quote uses parentheses in the way that throws JSLint warnings, e.g. (function() {})(); – Frankie Mar 22 '12 at 00:54
  • 1
    Is there an ignore setting for this? – streetlight Dec 17 '14 at 19:46