In many books / blog posts the self invoking anonymous function pattern is written like this:
(function() {
var foo = 'bar';
})();
However running a JSLint on this gives this error:
Move the invocation into the parens that contain the function.
e.g. changing it to this works:
(function() {
var foo = 'bar';
}());
Questions
- Why is the first implementation not good enough for JSLint? What are the differences?
- What is the preferred form? is JSLint always right?
- Why does it work? after all
function(){}()
throws aSyntaxError: Unexpected token (
But wrapping it with parens makes it all of a sudden work? e.g. (function(){}()
) - works fine
(After all this is JavaScript, not Lisp, so what is the effect of the wrapping parens on the ohterwise syntax error?)
EDIT: this is somewhat of a followup to this (I would not say exact duplicate though): JSLint error: "Move the invocation into the parens that contain the function", so my main question is #3, why does it work at all?
funbearable :) – Eran Medan Apr 16 '13 at 01:02