r/programming • • Sep 24 '15

Composition over Inheritance

https://medium.com/humans-create-software/composition-over-inheritance-cb6f88070205
92 Upvotes

48 comments sorted by

View all comments

Show parent comments

-2

u/faceinpale Sep 24 '15

JavaScript does not support mix-ins or traits as Self does. They are simulated by concatenating methods to an object used as a prototype. The limitation of this implementation is that changes to a mix-in are not propagated automatically to the augmented prototype.

Edit: grammar.

17

u/[deleted] Sep 24 '15 edited Sep 24 '15

Ok, so as I thought, excessively pedantic.

The point of me differentiating mixins from composition here is because both are possible in the context of JavaScript and are two distinct techniques with distinct properties.

In your case you came arguing mixins in JavaScript are impossible, so we should use another term, despite there's no confusion whatsoever what "mixin" refers to in the context of JavaScript. I also said this is a "more casual" way of referring to this mechanism, referring to popular JS use.

Let's see what Google thinks with a very specific search, to avoid any unintended noise:

https://www.google.com/search?q="JavaScript+mixins" : 2350 results

https://www.google.com/search?q="JavaScript+concatenative+prototyping" : 0 results

Don't be excessively pedantic. It's not very charismatic.

-7

u/faceinpale Sep 24 '15

The so called "mix-ins" in JS are usually applied to a prototype object, where the author of the article creates a new object every time. I cannot think of a term that is used in the JS community about this type of object creation so I used the one I already knew from the few papers I read on this topic. Also my understandings of the composition in programming suggest me that what the author describes is really composition.

"Don't be excessively pedantic. It's not very charismatic." - being an ass-hole, by describing someone with unpleasant words because of pure ignorance, is not charismatic either.

3

u/[deleted] Sep 24 '15 edited Sep 24 '15

The so called "mix-ins" in JS are usually applied to a prototype object

Good way to hedge that bet with a "usually" in there. You know it's both ways. :-)

Given a "prototype object" is just another object, and not a special kind of object, nothing stops the author from using his result as a prototype for new instances. Inheriting a prototype is orthogonal to mixing in properties by copying.

Both uses are common, this is why APIs like Object.assign(), jQuery.extend() etc. don't specifically merge into an object's prototype, this is left to the caller to decide.

Also my understandings of the composition in programming suggest me that what the author describes is really composition.

Object composition is a "has a" relationship for objects, as in one or more objects are subordinates and contained by another object. How can I put it so you don't twist my words, let me see... It's basically like a tree of objects:

trunk.thickBranch.thinBranch.leaf
ceo.vp.manager.seniordeveloper.developer.pizza
earth.america.usa.california.sanfrancisco

...You get the idea.

Each object in the tree has its own encapsulated state, they don't share the same state.

See the UML diagram and examples here: https://en.wikipedia.org/wiki/Object_composition

The author's examples show one object where methods share state. There's no identifiable "has a" relationship between the "composed" objects.

The author refers to these objects as composition:

barker + driver + killer

In fact, the only "composition" that's happening is this:

(murderRobotDog's methods) + state

Which is identical to creating murderRobotDog as a prototype once, and then instantiating it and using "this" for state. Using a prototype means you can monkey-patch the prototype afterwards & affect all instances, but no one in their right mind does that anymore, so for for all practical purposes they're equivalent... except the prototype solution is faster on V8 and SpiderMonkey, among others.

"Don't be excessively pedantic. It's not very charismatic." - being an ass-hole, by describing someone with unpleasant words because of pure ignorance, is not charismatic either.

Well if it's only when it's because of pure ignorance, I'm on the safe side here.

1

u/faceinpale Sep 24 '15

"Both uses are common, this is why APIs like Object.assign(), jQuery.extend() etc. don't specifically merge into an object's prototype, this is left to the caller to decide."

Actually you are right. If we call this technique a "mix-in" for prototypes the same goes for objects not used as prototypes too.

Mix-ins, as in Ruby and other languages that support that feature, are viewed as a type of inheritance. The main difference here, in JS, is that we are augmenting objects by appending functions to them, which are objects too, so an object and its methods can be viewed as a "has-a" relationship. If you know what kind of relation is the one between an object and its methods I would be grateful if you share that knowledge.

I tried to find what name for this type of "mix-ins" is used in other languages or in general but couldn't find any (other than the name I gave earlier). If you know such names once again I would be grateful if you share that knowledge. (and no, they are no real "mix-ins". "fake mix-ins" or "mix-in simulation" might be good names).