在 classfull-Style (c++) 或传统设计模式 (GofPatterns) 中,组合和继承之间的区别是什么、如何实现以及何时使用什么(优点/缺点)非常清楚。
但是如何区分 JS 中的组合或“正常”继承?或者它被用作相同的术语?
例如,原型(prototype)继承是否定义为组合或继承?
你们在想什么?
最佳答案
Is a prototype inheritance for example defined as a composition or inheritance?
What are you guys thinking?
这不是我在想什么,而是语言核心提供了什么......
in the classfull-Style (c++) or in the traditional Design Patterns (GofPatterns) it is really clear, what is the difference between composition and inheritance and how it is implemented and when to use what (advantages/disadvantages).
But how do you distingish between Composition or the "normal" Inheritance in JS? or it is used as the same term?
……当然,人们不应该过度扭曲概念。因此,对于 JavaScript 来说,组合和继承与支持这些范例的任何其他 PL 一样重要。 JavaScript 具有委托(delegate)功能,它已经启用了代码重用的两种变体。首先,继承,在 JavaScript 中由绑定(bind)到构造函数的 [prototype]
属性的委托(delegate)自动机制涵盖。其次,对象组合,它基于通过其调用方法之一([call]
或 [apply]
)显式委托(delegate)函数。
总结一下:
- 原型(prototype)委托(delegate)(自动)==继承
- 函数对象的显式委托(delegate)==基于 Angular 色的组合概念,例如Mixins和特质/人才<
- 逐步将属性从一个对象复制到另一个对象==实现混合组合的另一种方式
我已经在另外两个回复中提供了示例……
- stackoverflow.com::Traits in javascript
- stackoverflow.com::How to use mixins properly in Javascript
关于Javascript 区分组合与继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24288926/