re: Explain Inheritance in JavaScript Classes Like I'm Five VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Despite its complexity, probably one of the biggest advantages you get from prototypal inheritance is the fact that all properties and methods in t...

We're getting off tracks here, but composition don't duplicate anything. Try this:

const canSpeak = {
    speak: msg => console.log(msg),
};
const me = Object.assign({}, canSpeak);
me.speak("Hello"); // => Hello
const you = Object.assign({}, canSpeak);
console.log(me.speak === you.speak); // => true

Oh, I just wanted to mention a "feature" that comes with prototypal inheritance for the sake of discussion since this post is about prototypal inheritance in the first place.

Also, I never actually thought of composition that way. I didn't know that the two objects would end up referencing the same speak method. That's pretty cool. Thanks for the heads up!

code of conduct - report abuse