Closed elibarzilay closed 6 years ago
That’s pretty cool. A difference between this and what I have in the book is that this creates uses inheritance as the primary way of doing mixins (in that each if your classes extends another to gain its functionality) whereas mine is attempting to show that you can avoid using inheritance (outside of the mixin function) to gain new functionality.
Thanks for the feedback.
@nzakas, yes --- and this is a good thing to mention because (a) it uses
the proper inheritance so it can survive a possible future revision of
JS that might invalidate the direct approach (unlikely) or that will add
more benefits to the "proper" class
way; and (b) it's a common pattern
in some functional languages, so it's good to let JS people know about
it too...
Mixins are described in a way that is a bit ugly, since the example uses
prototype
which is depending on how classes are implemented. It's something that is unlikely to break, but there's a way to do the same without going down to the plumbing levels:[Disclaimer: this is the way it's done in some languages; I thought that someone must have done it in JS too, and sure enough, someone did.]