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

--

--

Father. Husband. Solutions developer profesionally (software quite often). Arsenal supporter. Cyclist.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Brodziak

Father. Husband. Solutions developer profesionally (software quite often). Arsenal supporter. Cyclist.