Javascript Distinguish composition versus inheritance

in classfull-Style (C ++) or in traditional design patterns (GofPatterns) it is really clear what the difference is between composition and inheritance and how it is implemented, and when to use what (advantages / disadvantages).

But how do you distinguish between composition or "normal" inheritance in JS? or is it used as the same term?

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

What do you guys think?

+1
javascript inheritance prototype composition
Jun 18 '14 at 3:01
source share
2 answers

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

What do you guys think?

This is not about what I think, but about what the core language gives ...

in classfull-Style (C ++) or in traditional design patterns (GofPatterns) it is really clear what the difference is between composition and inheritance and how it is implemented, and when to use what (advantages / disadvantages).

But how do you distinguish between composition or "normal" inheritance in JS? or is it used as the same term?

... and, of course, you should not bend concepts too much. Thus, for JavaScript, composition and inheritance are also taken into account as for any other PL that supports these paradigms. JavaScript Features Delegation, which already includes two options for reusing code. Firstly, Inheritance, which in JavaScript is covered by delegation automation related to the [prototype] property of constructor functions. Secondly, Object Composition, based on the explicit delegation of a function through one of its call methods ( [call] or [apply] ).

Summarizing:

  • Prototype deletion (automatism) == Inheritance
  • Explicit delegation of functional objects == Role-based composition concepts like Mixins and Traits / Talents
  • phased copying of properties from one object to another == another way to achieve the composition of the mixture.

I already gave examples in two other answers that ...

  • stackoverflow.com :: Features in javascript
  • stackoverflow.com :: How to properly use mixins in Javascript
+1
04 Oct '14 at 2:14 on
source share

When I first learned Java, I was tempted to try to do the same with Javascript. But that was the wrong idea. Javascript allows you to program oriented objects. The OOP method runs with Javascript different from C ++ or Java: this is how you use it, which allows OOP, not the language itself.

I recommend you these links to learn how to use Javascript correctly:

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

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

0
Jun 18 '14 at 15:09
source share



All Articles