Alright, I know what's the difference between a FunctionDeclaration
and a FunctionExpression
. @CMS did a great job of explaining that. Till recently, I used to use the same syntax for creating lambda expressions and namespaces following Douglas Crockford's example:
(function () {
"use strict";
}());
He justifies this convention as follows:
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.
This syntax is great for lambda expressions because they return a value. However, namespaces do not return a value. Hence I now prefer using the following syntax for namespaces instead (taken from the JavaScript Garden):
(function () {
"use strict";
})();
The rationale behind the use of this syntax is given as follows:
( // evaluate the function inside the paranthesis
function() {}
) // and return the function object
() // call the result of the evaluation
Using this syntax for namespaces makes more sense to me because: this syntax separates the namespace and the invocation; and Douglas Crockford's syntax expects the function to return a value.
However, when I pass this syntax through JSLint I get the an error stating "Move the invocation into the parens that contain the function.
". I don't understand why it complains about this syntax. It's a widely used and accepted syntax.
I plan on notifying Douglas Crockford about this problem. However, before I do so I would appreciate any feedback you may provide. Perhaps it's better to simply stick with his syntax? Please explain your views.