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.
In retrospect I believe Crockford’s views are simply dogma. He says:
Well my arguments is that when a function is being invoked immediately how is it not clear that the value being produced is the result of the function and not the function itself? Consider:
The value being produced is the function:
The value being produced is the result of the function:
If you want the value being produced to be the function itself then simply don’t use parens. It doesn’t make sense to use them anyway.
Understandably there are many programmers who would still wrap a function expression in parentheses before assigning it to a variable, or who would be a sadist and do something like this to confuse Doug:
However how difficult is it to understand what’s going on? Perhaps in the last example you would need to scroll down several hundreds of lines of code to find out but a good programmer would save others the trouble and just wrap the function in parens; or if he’s a miser:
Beside I wouldn’t even bother reading code written by someone who’s hell bent on making me curse him. Coming back to the question though the reason I really dislike Crockford’s syntax is because he’s grouping the function and invocation in a single parentheses. To me that’s equivalent to the following:
Obviously wrapping a function invocation in parens looks a little silly and redundant but that’s how I imagine it to be and it always irks me, but every man to his own way. Personally I think my syntax looks cleaner.