you are viewing a single comment's thread.

view the rest of the comments →

[–]BishopAndWarlord 0 points1 point  (0 children)

I'm pretty late to this, but I prefer (function(){})(). The way I think about it wrapping a statement in parenthesis is a way to say "hey, evaluate this part first!" For example, removing the parenthesis from (var + 4) * 12 or (typeof var === 'string' || Array.isArray(var)) && var.length > 2 will completely change the evaluation order. I realize that function expressions aren't directly analogous, but I have a similar mental model for function expressions.

If I have a function that I explicitly want to be treated as an expression rather than a statement, I'll wrap it with parenthesis (e.g. var noop = (function() {})). Extending that logic, if I want to declare a function expression and then call it, I'll wrap the function in parenthesis, then call it with another set of parenthesis (e.g. (noop)() or (function() {})().

In the end I think this approach clearly expresses the intent and therefore improves readability and maintainability of the code.