Javascript Distinguish between Composition vs. Inheritance
Asked Answered
L

3

1

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?

Lynda answered 18/6, 2014 at 15:1 Comment(7)
It is JavaScript's implementation of inheritance. It's basically a lookup chain of objects. When you request a property from an object, if the object doesn't have it, it looks at the next object in its prototype chain, and so on until it finds the property or reaches the end of the chain. Many objects can and do inherit from a single object.Mozambique
...or is it entirely a semantic question? Are you asking if JavaScript's prototypal inheritance could also be described as composition?Mozambique
Sry i asked not clearly... Thx, but I know how prototype inheritance , the prototype chain and also OOP in js works... Yes, you are right @cookie monster, its the entire semantic question you defined :)... When someone asks me how composition vs. Inheritance in js works, can i say that the prototype inheritance is a way of composition or should i say there is no distinguish between composition and general inheritance (with f.e. extend)... How would you answer this question as an engineer?Lynda
It's an interesting question. I guess I've never really heard composition discussed in the context of JS, but I guess its inheritance model could be seen as being built using composition in a sense, since the prototype of an object is a separate object with its own properties, like an implicit linked list. However, when utilizing "inherited" properties, they operate as though they are part of the actual object, for example the value of this in an inherited method being the actual object instead of the method owner. So it all seems a bit muddled together. I guess I don't have a clear answer.Mozambique
...I wonder if the programmers.stackexchange.com/help/on-topic site would be a better place for this question. But generally it's just thought of as JavaScript's inheritance model.Mozambique
Great answer, thx... I thought the same way ;) and have not found anything about this question... the question came to me cause in one week i have my bachelor exam and a possible question could be "js composition vs. Inheritance"... So i was a little bit confused about this terms... Thank you very much, @cookie monsterLynda
Ok i will keep this site in my mind for future questions about general software engineering or developingLynda
K
1

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 …

Kieger answered 4/10, 2014 at 2:14 Comment(0)
V
0

As I first learned Java, I was tempted to try to do the same thing with Javascript. But it was a wrong idea. Javascript allows to do oriented-object programming. 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.

I recommend you these links to properly learn about how Javascript can be used:

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

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

Viccora answered 18/6, 2014 at 15:9 Comment(1)
Thx for your help but i know how OOP in js works... It's more a question if js experts and js software engineers distinguish between the two terms composition and inheritance or if they always just call it inheritance , no matter the way it is reachedLynda
D
-2
<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>
Desirous answered 18/8, 2022 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.