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 individual Counter
needs to track a certain event, but wherein you also want a protected TotalCountEvents
across all Counter
objects, you could do this:
var Counter = (function() {
var totalcount = 0;
var constructor = function() {
var count = 0;
this.increment = function() { count += 1; totalcount += 1; };
this.getCount = function() { return count; };
};
constructor.getTotalCount = function() { return totalcount; };
return constructor;
})();
You can then use it like this:
var counterA = new Counter();
var counterB = new Counter();
counterA.increment(); counterA.increment();
counterB.increment(); counterB.increment(); counterB.increment();
console.log(counterA.getCount(), counterB.getCount(), Counter.getTotalCount());
And you'll see:
> 2 3 5
But, access to the underlying totalcount
will be restricted to instances of Counter
.
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.