The difference between classic and prototype inheritance

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

Don't you like it? Does this definition mean? For me, in both cases, the constructor is used (with a new one you can override the returned object, that's all), and in both cases there is a prototype inheritance. Am I missing something or is the definition above not very accurate?

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

Inheritance of classes: inherits from classes (for example, a plan - a description of a class) and creates relationships between subclasses: hierarchical class taxonomies. Instances are usually created using constructor functions with the new keyword. Class inheritance may or may not use the class keyword from ES6.

Inheritance Prototypal Inheritance: inherits directly from other objects. Instances are usually created using the factory or Object.create() functions. Instances can be composed of many different objects, making it easy to selectively inherit. *

+6
source share
2 answers

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

In addition to inheriting properties, class inheritance makes additional wiring to bind the child [[Prototype]] to the parent [[Prototype]]. Usually, the super() constructor is also called. These additional steps form the parent / child hierarchies and create the narrowest connection available in the OO design.

Therefore, "Classes inherit from classes and create subclass relationships: taxonomy of a hierarchical class. "

It is also useful to understand that there is more than one kind of prototype OO. It is important to note that concatenative inheritance and delegation of prototypes .

Concatenative inheritance is important because it allows a simple (and very general) composition of objects in JavaScript. Remember that the Gang of Four said, "Thank the composition of the object over the inheritance of the class."

This is the common wisdom of OO design, and because of concatenative inheritance, this is easy to do in JavaScript.

See "JavaScript Interview Wizard: What's the Difference Between a Class and an Inheritance Prototype?"

+3
source

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

Interface difference

How to use it in code. The difference and advantages are well explained in the article.

Semantic difference

Regardless of how it is implemented in javascript, we can use the ES6 class to emphasize that our object has a value of "class". Initially, "class" means that we can class ify some object to a particular set of objects. See Definition in Set Theory: https://en.wikipedia.org/wiki/Class_(set_theory) .

In addition, the class is something abstract and does not exist before we instantiate it.

If we are talking about class inheritance, just understand the abstraction that some class may be a subclass of another class that creates a hierarchy.

A prototype is a sample or a representative object from a certain set of objects. in this case, we create new objects using the existing prototype (create a clone or link). And they can also be prototypes for new facilities.

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

+4
source

All Articles