1 min readJun 5, 2020
Why bother with all the caveats of constructor in JavaScript if you can write a factory function that will return an object instead? It has got the same effect, but skips new
insanity. This is how simple it is:
const createPerson = ({ name }) => ({
name,
setName(name) {
this.name = name;
return this;
}
});
const joe = createPerson({ name: 'Joe Doe' });
The trick with instanceof
is clever, but due you really need such tricks all over the place?
I’ve presented factory pattern and other useful ones in my post https://levelup.gitconnected.com/oop-best-practices-that-are-anti-patterns-in-functional-javascript-61ee1af35452