The module pattern isn't only (or even primarily) used to mimic classes. It's proper modules, not classes, that make the old module pattern likely to become...if not obsolete, then a lot less-used, in the next few years. Those and private methods (which are currently a Stage 3 proposal) and perhaps to an extent private fields (also Stage 3).
But prior to proper modules, or sometimes even concurrent with them, you'd still use it if you want to have private functions or data or similar related to something which isn't private. Example:
const Foo = (function() {
function privateFunction(x) {
return /*...do something with `x`...*/;
}
class Foo {
dosomething(x) {
return privateFunction(x) ? "a" : "b";
}
}
return Foo;
})();
I'll also note that sometimes, you might use an anonymous block rather than an IIFE, since class
, const
, and let
have block scope. I wouldn't above because I'd have to make the outer Foo
non-constant (so I could assign to it in the block), but there are other times when it might apply...