JavaScript Mixins for grown-ups
javascriptweblog.wordpress.com
JavaScript Mixins for grown-ups
1–8 of 8 posts
Re: JavaScript Mixins for grown-ups
#2JavaScript can't do multiple inheritance and so you have to hack mixins by copying. This sucks when you want to e.g. extend an Enumerable mixin because.. well, you can't.
Re: JavaScript Mixins for grown-ups
#3Re: JavaScript Mixins for grown-ups
#4Note that the way mixins generally work in languages with first class support for them is with multiple inheritance. That is, mixins are included by reference and, in a dynamic language, changes to the mixin affect objects that have already been extended. JavaScript can't do multiple inheritance and so you have to hack mixins by copying. This sucks when you want to e.g. extend an Enumerable mixin because.. well, you…
You don't have to "hack mixins by copying" in Javascript. And what did you mean by the last sentence?
Re: JavaScript Mixins for grown-ups
#5Note that the way mixins generally work in languages with first class support for them is with multiple inheritance. That is, mixins are included by reference and, in a dynamic language, changes to the mixin affect objects that have already been extended. JavaScript can't do multiple inheritance and so you have to hack mixins by copying. This sucks when you want to e.g. extend an Enumerable mixin because.. well, you…
Uh, can you elaborate more? Mixins work just fine in Ruby which has no multiple inheritance. You don't have to "hack mixins by copying" in Javascript. And what did you mean by the last sentence?
Specific quote my reasoning is based on: ... changes to the mixin affect objects that have already been extended.
edit: well... you could. By making mixin objects that propagate changes to themselves to instances of things which include them - pass each instance to the mixin, and keep a list. But that'd end up making your entire program into one big memory leak, unless you want to go down the constructor/destructor and manual memory management route in JS. I doubt it's worth it.
Re: JavaScript Mixins for grown-ups
#6Note that the way mixins generally work in languages with first class support for them is with multiple inheritance. That is, mixins are included by reference and, in a dynamic language, changes to the mixin affect objects that have already been extended. JavaScript can't do multiple inheritance and so you have to hack mixins by copying. This sucks when you want to e.g. extend an Enumerable mixin because.. well, you…
Proxies, which are only supported currently in Firefox but should see support in other browsers eventually, will allow you to intercept all types of property access/manipulation on objects. This will allow generalized multiple inheritance and dynamic mixins; I wrote basically that using node-proxy for Node.js.
Re: JavaScript Mixins for grown-ups
#7Note that the way mixins generally work in languages with first class support for them is with multiple inheritance. That is, mixins are included by reference and, in a dynamic language, changes to the mixin affect objects that have already been extended. JavaScript can't do multiple inheritance and so you have to hack mixins by copying. This sucks when you want to e.g. extend an Enumerable mixin because.. well, you…
JS's object modell is prototype based, so there's no traditional inheritance, just the chain of prototypes. And you can monkey patch it any way you like at any level. (Adding currying to Function.prototype is a nice and scary example of this.)
Directly defining functions into the prototype chain doesn't even require copying and you can put an arbitrary number of functions there. And you can just redefine the Enumerable mixin's functions, or wrap then into your functions, so extending isn't a problem.
Re: JavaScript Mixins for grown-ups
#8Note that the way mixins generally work in languages with first class support for them is with multiple inheritance. That is, mixins are included by reference and, in a dynamic language, changes to the mixin affect objects that have already been extended. JavaScript can't do multiple inheritance and so you have to hack mixins by copying. This sucks when you want to e.g. extend an Enumerable mixin because.. well, you…
> JavaScript can't do multiple inheritance JS's object modell is prototype based, so there's no traditional inheritance, just the chain of prototypes. And you can monkey patch it any way you like at any level. (Adding currying to Function.prototype is a nice and scary example of this.) Directly defining functions into the prototype chain doesn't even require copying and you can put an arbitrary number of functions th…
Because JS objects can only have a single prototype, you can't inherit from multiple places orthogonally. If you insert mixins into the prototype chain then they can't be shared between multiple chains, and so aren't really mixins.