They're optional in that code, this also works fine:
var NOW = function(){
var privateVariable = 5;
var privateFunction = function(){};
return{
publicVariable: 4,
publicFunction: function(){}
};
}();
People are in the habit of wrapping functions that they're going to immediately invoke in ()
because if you aren't assigning the result to something (or similar), it's necessary to tell the parser that the function
keyword is starting a function expression rather than a function declaration. E.g., this is not okay:
function() { alert("foo"); }();
Because when the parser encounters function
where it could be expecting a statement, declaration, or expression, it assumes it starts a declaration. So we do this:
(function() { alert("foo"); })();
or this:
(function() { alert("foo"); }());
or any of several other things (see this question's answers) to put the parser in the mode where it's expecting an expression, not a statement or declaration.