What is the benefit of the Javascript Module Pattern?
<p>I have been doing research to come up with a standardized Javascript coding style for my team.  Most resources now recommend the "Module" pattern that involves closures, such as this:</p>

<pre><code>var Module = function() { 

    someMethod = function() { /* ... */ };

    return { 
        someMethod: someMethod
    };

}();
</code></pre>

<p>and invoke it like <code>Module.someMethod();</code>.  This approach seems to only work with methods that would be static in a traditional OOP context, for example repository classes to fetch/save data, service layers to make outside requests, and the like.  Unless I missed something, the module pattern isn't intended to be used with data classes (think DTOs) that would typically need to be passed to/from the service methods to the UI glue code.</p>

<p>A common benefit I see cited is that you can have true private methods and fields in Javascript with the module pattern, but this can also be achieved <em>along with</em> being able to have static <strong>or</strong> instance methods with the "classical" Javascript style similar to this:</p>

<pre><code>myClass = function(param) { 
    // this is completely public
    this.publicProperty = 'Foo';

    // this is completely private
    var privateProp = param;

    // this function can access the private fields
    // AND can be called publicly; best of both?
    this.someMethod = function() { 
        return privateProp;
    };

    // this function is private.  FOR INTERNAL USE ONLY
    function privateMethod() { 
        /* ... */
    };
}

// this method is static and doesn't require an instance
myClass.staticMethod = function() { /* ... */ };

// this method requires an instance and is the "public API"
myClass.prototype.instanceMethod = function() { /* ... */ };
</code></pre>

<p>So I guess my question is what makes the Module Pattern better than the traditional style?  It's a bit cleaner, but that seems to be the only benefit that is immediately apparent; in fact, the traditional style seems to offer the ability to provide real encapsulation (similar to true OOP languages like Java or C#) instead of simply returning a collection of static-only methods.</p>

<p>Is there something I'm missing?</p>
