Common Misconceptions About Inheritance in JavaScript
Eric Elliott
2.3K68

I’m new to JavaScript coming from OO background and still trying to find the best (better) way to use it, so have a few questions:

  1. I think I understand the motions of Object.assign/Object.create, but still can’t understand how it solves the problem of deep hierarchies. You can have 6 level hierarchy through extends, you can build exactly the same hierarchy through Object.assign and Object.create, right? Why is it better?
  2. The way you see composition looks like you suggest having more than one parent prototype through Object.assign which looks very much like multiple inheritance which has been labelled as dangerous and removed from OO languages. Why do you think it’s safe to do it in JavaScript? (just thinking about Crockford’s quote about somewhat dangerous features you should avoid) Specifically, skydivingNinjaMouseWithWingsuit looks like a Frankenstein you don’t really want to see in an application, it’s not even possible to predict what it will be doing, right?
  3. Animal/mouse example seems to be a too simple example for this discussion. I’ve just done it via classes/extends to compare and the code actually looks cleaner: a. you have just “extends” instead of “return Object.assign(Object.create….”; b. you get less curly-brace nesting; have you came across an example of moderate complexity which demonstrates pros of this approach over classes? I’m specifically asking about moderate complexity example, since frameworks like express which you can surely look into, do not seem to be something you can interrogate pretty quickly.

Thanks for the article, BTW, it explains how to do prototype inheritance very well.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.