Javascript区分组合与继承 [英] Javascript Distinguish between Composition vs. Inheritance

查看:123
本文介绍了Javascript区分组合与继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在classfull-Style(c ++)或传统的设计模式(GofPatterns)中,非常清楚,组合和继承之间有什么区别,它是如何实现的以及何时使用(优点/缺点)。



但是你如何在组合或JS中的正常继承之间徘徊?或者它被用作相同的术语?



原型继承是例如定义为组合还是继承?



<你有什么想法?

解决方案


原型继承例如被定义为一个组合还是继承?



你们在想什么?


它不是关于我在想什么,但语言核心提供了什么...


在classfull-Style(c ++)或传统的Design Patterns( GofPatterns)很清楚,组合和继承有什么区别,它是如何实现的以及何时使用(优点/缺点)。



但你怎么做组合之间或JS中的正常继承?或者它被用作相同的术语?


...当然,不应该将概念弯曲得太多。因此对于JavaScript来说,组合和继承的数量与支持这些范例的任何其他PL一样多。 JavaScript功能委派,它已经启用了代码重用的两种变体。首先,继承,它在JavaScript中被委托自动化所涵盖,该委托自动化绑定到构造函数的 [prototype] 属性。其次,对象组合,它基于通过其中一种调用方法显式委派一个函数( [call] [apply] )。



总结:




  • 原型委派(自动)==继承

  • 明确授权功能对象== 基于角色的组合概念,如 Mixins Traits / Talents

  • 逐步将属性从一个对象复制到另一个对象==另一种实现mixin组合的方式



我已经在其他两个回复中提供了示例...




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?

Is a prototype inheritance for example defined as a composition or inheritance?

What are you guys thinking?

解决方案

Is a prototype inheritance for example defined as a composition or inheritance?

What are you guys thinking?

It is not about what I’m thinking but what the language core provides …

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?

… and of course one shouldn't bend concepts to much. Thus for JavaScript too, composition and inheritance count as much as for any other PL that supports these paradigms. JavaScript features Delegation, which already enables two variants of Code-reuse. Firstly, Inheritance, which in JavaScript is covered by a delegation automatism that is bound to the [prototype] property of constructor functions. Secondly, Object Composition, which is based on explicitly delegating a function via one of it's call methods ([call] or [apply]).

Summing it up:

  • Prototypal Delegation (Automatism) == Inheritance
  • Explicit Delegation of Function Objects == Role based composition concepts like Mixins and Traits / Talents
  • stepwise copying properties from one objects to another == another way of achieving kind of mixin composition

I already did provide examples in two other responses that are …

这篇关于Javascript区分组合与继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆