简体   繁体   English

Javascript 区分组合与 Inheritance

[英]Javascript Distinguish between Composition vs. Inheritance

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

But how do you distingish between Composition or the "normal" Inheritance in JS?但是你如何区分组合或 JS 中的“正常”Inheritance? or it is used as the same term?或者它被用作同一个术语?

Is a prototype inheritance for example defined as a composition or inheritance?例如,原型 inheritance 是否定义为组合物或 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). 在classfull-Style(c ++)或传统设计模式(GofPatterns)中,确实很清楚,组合和继承之间的区别是什么,如何实现和何时使用(优点/缺点)。

But how do you distingish between Composition or the "normal" Inheritance in JS? 但是,如何区分JS中的Composition或“常规”继承呢? 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而言,组成和继承也与支持这些范例的任何其他PL一样重要。 JavaScript features Delegation , which already enables two variants of Code-reuse . JavaScript具有委派功能,它已经启用了Code-reuse的两种变体。 Firstly, Inheritance , which in JavaScript is covered by a delegation automatism that is bound to the [prototype] property of constructor functions. 首先, 继承(Inheritance )在JavaScript中由委派自动机覆盖,该自动机绑定到构造函数的[prototype]属性。 Secondly, Object Composition , which is based on explicitly delegating a function via one of it's call methods ( [call] or [apply] ). 其次, Object Composition ,它基于通过其调用方法之一( [call][apply] )显式委派一个函数的基础。

Summing it up: 总结一下:

  • Prototypal Delegation (Automatism) == Inheritance 原型委托(自动)==继承
  • Explicit Delegation of Function Objects == Role based composition concepts like Mixins and Traits / Talents 功能对象的显式委派==基于角色的构图概念,例如MixinsTraits / 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 … 我已经在另外两个响应中提供了示例,它们是……

As I first learned Java, I was tempted to try to do the same thing with Javascript. 在我第一次学习Java时,我很想尝试使用Javascript做同样的事情。 But it was a wrong idea. 但这是一个错误的想法。 Javascript allows to do oriented-object programming. Javascript允许进行定向对象编程。 The way OOP is done with Javascript is different from C++ or Java: it is the way you use it that allows OOP, not the language itself. 用Javascript完成OOP的方式不同于C ++或Java:使用OOP允许使用OOP的方式,而不是语言本身。

I recommend you these links to properly learn about how Javascript can be used: 我建议您使用以下链接以正确了解如何使用Javascript:

http://javascriptissexy.com/how-to-learn-javascript-properly/ http://javascriptissexy.com/how-to-learn-javascript-properly/

http://javascriptissexy.com/learn-intermediate-and-advanced-javascript/ http://javascriptissexy.com/learn-intermediate-and-advanced-javascript/

<script> 
    //Composition is one class is compsed of many objects of other classes. Composition is HAS-A relationship or containment
    //Inheritance is extension of super class IS-A relationship or specialization 

    class Addition{
        add(a,b){
            return a+b;
        }
    }
    class Subtraction{
        subtract(a,b){
            return a-b;
        }   
    }
    class Multiplication{
        multiply(a,b){
            return a*b;
        }   
    }
    class Division{
        divide(a,b){
            return a/b;
        }   
    }
    ///////////////////
    class CalculatorTest{
        constructor(a,b)
        {
            this.a=a;
            this.b=b;       
        }
        show(){
            let objAdd=new Addition();
            console.log("Addition:"+objAdd.add(this.a,this.b));
            let objSubtract=new Subtraction();
            console.log("Subtraction:"+objSubtract.subtract(this.a,this.b));
            let objMultiply=new Multiplication();
            console.log("Addition:"+objMultiply.multiply(this.a,this.b));
            let objDivide=new Division();
            console.log("Addition:"+objDivide.divide(this.a,this.b));

        }
    }
    let c1=new CalculatorTest(10,10);
    c1.show();
    </script>

//Inheritance
<script>
    //Inheritance Example
    //Super Class
    class Addition{
        add(a,b){
            return a+b;
        }
    }

    //Sub Class extends the functionality of super class
    class CalculatorTest extends Addition{
        constructor(a,b)
        {
            super();
            this.a=a;
            this.b=b;
        }
        show(){
            console.log(this.add(this.a,this.b));
        }
    }
    let c1=new CalculatorTest(10,10);
    c1.show();

    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM