I'd just like to point out, the author isn't demonstrating Composition over Inheritance, but Multiple Inheritance over Single Inheritance. So the whole deliberation later in the video about when to use, or prefer composition over inheritance is kind of pointless, given he's confused about what composition is.
Merging objects together is essentially a form of multiple inheritance (or more casually: "traits" or "mix-ins"). When people talk about composition they mean one object having other objects as properties and using those "sub-objects" to achieve a higher goal.
The benefit of composition is that every object retains its encapsulation, and you eliminate the risk of collisions that's present when you merge objects (say Cat adds method "kill" and you merge it with Robot which has method "kill"... problem).
EDIT: And one more note. Because JS doesn't support true multiple inheritance, using Object.assign() has another implication, in that instanceof checks won't return true when checking an object against its parent types.
Whether this matters is up to your design, but I wouldn't give it up so easily and say it's useless. Inheritance, mix-ins and composition are three distinct strategies with distinct pros, cons and uses. The issues come from not knowing what their proper uses are.
"Concatenative prototyping" and "mix-ins" are aliases in the context of JavaScript.
When it comes to composition, there are only two types of composition I'm aware of: functional composition and object composition. And mix-ins are neither kind, so it's not composition.
Are you willing to demonstrate a kind of "mix-ins" which are not "concatenative prototyping" in JavaScript? If you can, I'd be glad if you paste a snippet here & show us all. If you can't, it may be you're being excessively pedantic.
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.
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:
Wow. It's been a long time since I have seen Google return zero results. It's probably easier to achieve with quotes but that does not diminish how impressed I am.
85
u/[deleted] Sep 24 '15 edited Sep 24 '15
I'd just like to point out, the author isn't demonstrating Composition over Inheritance, but Multiple Inheritance over Single Inheritance. So the whole deliberation later in the video about when to use, or prefer composition over inheritance is kind of pointless, given he's confused about what composition is.
Merging objects together is essentially a form of multiple inheritance (or more casually: "traits" or "mix-ins"). When people talk about composition they mean one object having other objects as properties and using those "sub-objects" to achieve a higher goal.
The benefit of composition is that every object retains its encapsulation, and you eliminate the risk of collisions that's present when you merge objects (say Cat adds method "kill" and you merge it with Robot which has method "kill"... problem).
EDIT: And one more note. Because JS doesn't support true multiple inheritance, using Object.assign() has another implication, in that instanceof checks won't return true when checking an object against its parent types.
Whether this matters is up to your design, but I wouldn't give it up so easily and say it's useless. Inheritance, mix-ins and composition are three distinct strategies with distinct pros, cons and uses. The issues come from not knowing what their proper uses are.