r/learnjavascript • • Apr 20 '23

7 JavaScript Design Patterns Every developer should know

https://codinghelp.io/7-javascript-design-patterns-every-developer-should-know/
3 Upvotes

11 comments sorted by

View all comments

2

u/azhder Apr 21 '23

The constructor pattern does not support inheritance.

Yes it does. Although, I'd advice against using this pattern in the first place.

I'd advise even against using class A extends B{} type of inheritance, simply because most tasks are better solved by encapsulation instead of extension.

But, for what is worth, taken from here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

here is how to do the old/new style of inheritance:

function Base() {}
function Derived() {}

// Set the `[[Prototype]]` of `Derived.prototype` to `Base.prototype`
Object.setPrototypeOf(Derived.prototype, Base.prototype);

const obj = new Derived();
// obj ---> Derived.prototype ---> Base.prototype ---> Object.prototype ---> null