3 ms·
The article describes a way to avoid the use of 'new' entirely, but you can just as easily get the advantages of factory encapsulation without the use of Object
by couchand 12y ago
The article describes a way to avoid the use of 'new' entirely, but you can just as easily get the advantages of factory encapsulation without the use of Object.create.
var MyModule = (function() {
function OneImplementation() {}
function OtherImplementation() {}
function factoryMethod() {
if (foobar) {
return new OneImplementation();
}
else {
return new OtherImplementation();
}
}
return {
create: factoryMethod
};
})();
As long as the use of new doesn't leak out of the module you're getting all of the advantages. Upgrade to Object.create when you get the chance, but don't let that hold you back.