Some articles (JavaScript Module Pattern In Depth, Mastering The Module Pattern) describe defining modules in JavaScript like in the snippet below (from Addy Osmani's "Learning JavaScript Design Patterns"):
var testModule = (function () {
var counter = 0;
return {
incrementCounter: function () {
return counter++;
},
resetCounter: function () {
console.log( "counter value prior to reset: " + counter );
counter = 0;
}
};
})();
Module usage:
testModule.incrementCounter();
testModule.resetCounter();
In this case we are using a single instance in the entire code, and it means that this module implementation is useful only if we want to create a singleton.
Is it true or there are other use cases when this Module pattern variation can be used?
Best Answer
I'd argue no-ish. I'm not sure it's really the "module pattern" if you return anything other than a singleton. But, you can certainly use the same "pattern" to accomplish other things.
The module protects the global scope from the internal variables used to build the return-value; but the return value can itself be a function -- including a constructor.
To that end, if you had a need for some sort of "private static" class member which you'd like all instances of a class to access, the module pattern can accomplish that.
As a silly example, suppose you're building a
Counter
class, wherein each individualCounter
needs to track a certain event, but wherein you also want a protectedTotalCountEvents
across allCounter
objects, you could do this:You can then use it like this:
And you'll see:
But, access to the underlying
totalcount
will be restricted to instances ofCounter
.Bear in mind, the factory pattern can accomplish the same sort of thing, just with stylistic/syntactic differences. Changing your factory to look like a "module" mostly allows you to create instances with
new
... which is taboo in some circles anyway.