1

The Closure section of chapter 4 has the following example.

var myObject = (function() {
    var value = 0;

    return {
        increment: function(inc) {
            value += typeof inc === 'number' ? inc : 1;
        },
        getValue: function() {
            return value;
        }
    }
}());

Why is the function wrapped in an extra set of parenthesis? I've tested it without the parenthesis and the behavior is the same. Am I missing something here, or am I taking this too literally?

James Hibbard
  • 16,490
  • 14
  • 62
  • 74
MrMcKizzle
  • 150
  • 2
  • 10
  • It's a preference thing. It's primarily done for readability. This answer explains it more: http://stackoverflow.com/questions/6645766/why-are-parenthesis-used-to-wrap-a-javascript-function-call – snollygolly Jan 30 '15 at 21:58

0 Answers0