最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between classical inheritance and prototype inheritance - Stack Overflow

programmeradmin2浏览0评论

I found this definition here : .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 posed from many different objects, allowing for easy selective inheritance.*

I found this definition here : https://medium./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 posed from many different objects, allowing for easy selective inheritance.*

Share Improve this question edited Jan 18, 2016 at 12:46 Dmitry Yaremenko 2,58022 silver badges31 bronze badges asked Jan 10, 2016 at 16:07 François RichardFrançois Richard 7,06511 gold badges46 silver badges82 bronze badges 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. – jfriend00 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. – MinusFour 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. – François Richard Commented Jan 10, 2016 at 16:14
  • I think the author wanted to separate position from litteral inheritance – François Richard 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. – Ahmet Cetin Commented Jan 10, 2016 at 16:32
 |  Show 3 more ments

2 Answers 2

Reset to default 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/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.

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 mon) object position in JavaScript. Remember the Gang of Four said, "favor object position 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?"

发布评论

评论列表(0)

  1. 暂无评论