7

I found this definition here : https://medium.com/javascript-scene/10-interview-questions-every-javascript-developer-should-know-6fa6bdf5ad95#.y0nc8kx34

Doesn't it sound awkward to you ? Does this definition make sense ? For me in both case there is a use of a constructor (with new you can override the returned object that's all) and in both case there is a prototype inheritance. Am I missing something or the definition above is not really accurate ?

*3. What is the difference between classical inheritance and prototypal inheritance?

Class Inheritance: instances inherit from classes (like a blueprint — a description of the class), and create sub-class relationships: hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the new keyword. Class inheritance may or may not use the class keyword from ES6.

Prototypal Inheritance: instances inherit directly from other objects. Instances are typically instantiated via factory functions or Object.create(). Instances may be composed from many different objects, allowing for easy selective inheritance.*

8
  • This is not right at all. Even using the class syntax in ES6, it is still prototypal inheritance. The difference is not about the declaration syntax used, but about how it actually works in practice. Commented Jan 10, 2016 at 16:11
  • Well, it does say "typically". There's no classical inheritance in JS though, just syntax that hides prototypal inheritance. Commented Jan 10, 2016 at 16:14
  • I know right. I feel the author does not really understand how javascript inheritance works and mix it with other more typical OO languages knowledge but I want to be sure I'm not missing anything before blaming him. Commented Jan 10, 2016 at 16:14
  • I think the author wanted to separate composition from litteral inheritance Commented Jan 10, 2016 at 16:19
  • 1
    Well, I can assure you that Eric Elliot understands well how inheritance works in JS, just trying to show difference with other languages using classical inheritance. only problem in JS that you cannot have private variables internal to the class definition. you can achieve it only with functional inheritance having cost of memory for repeated methods in each instance. Commented Jan 10, 2016 at 16:32

2 Answers 2

4

There are interface and semantic differences between "class" and "prototype".

Interface difference

How to use it in the code. Difference and benefits well explained in the article.

Semantic difference

No matter how it's implemented in javascript, we can use ES6-class to emphasize that our object has the "class" meaning. Originally "class" means that we can classify some object to one or another set of objects. See definition in set theory: https://en.wikipedia.org/wiki/Class_(set_theory) .

Also, class is something abstract and not exists before we create an instance.

If we talk about class inheritance - it's simple to understand the abstraction that some class can be a sub-class of another class creating hierarchy.

Prototype is a sample or representative object from some set of objects. in that case we create new objects using existing prototype (creating clone or link). And they also can be prototypes for new objects.

When other programmers will read your code and see what you choose - prototype or class, they expect those semantic meanings.

Sign up to request clarification or add additional context in comments.

1 Comment

class and types are not analogous at all in JavaScript. Programmers expect them to be, which is a common source of confusion.
3

In JavaScript, class inheritance is implemented on top of prototypal inheritance, but that does not mean that it does the same thing:

In addition to inheriting properties, class inheritance does extra wiring to link the child [[Prototype]] to the parent [[Prototype]]. Usually, the super() constructor is also called. Those extra steps form parent/child hierarchies and create the tightest coupling available in OO design.

Hence, "Classes inherit from classes and create subclass relationships: hierarchical class taxonomies."

It's also useful to understand that there is more than one kind of prototypal OO. Importantly, there is concatenative inheritance, and prototype delegation.

Concatenative inheritance is important, because that's what allows for simple (and very common) object composition in JavaScript. Remember the Gang of Four said, "favor object composition over class inheritance."

This is generally accepted OO design wisdom, and because of concatenative inheritance, it's a breeze to do that in JavaScript.

For a lot more detail, see "Master the JavaScript Interview: What's the Difference Between Class and Prototypal Inheritance?"

3 Comments

Thank's for respond! So, prototypal inheritance doesn't create a link to the parent [[Prototype]]? And it means that I can't create deep hierarchy of classes/objects with prototypal?
What about mention about semantic difference? Do you agree with my answer?
Concatenative inheritance does not create a hierarchy of classes/objects, no. It is possible to create such hierarchies with prototype delegation, which is how class inheritance is implemented in JavaScript. Yes, there is a semantic difference between class and yes, I agree with your definition, with the caveat that class and types are not analogous at all in JavaScript. Programmers expect them to be, which is a common source of confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.