I'm coming up with View (HTML markup) and Utility (JavaScript - behavior) architecture and creating atomic classes for composing views and utilities using ES6 Class. There will be a need that multiple utility classes can be composed/mixed into a single view class.
How can ES6 Class API provide a way to mix-in class(es) into another/main class. I've looked at Object.assign
but that is for objects and not at class-level.
JavaScript classes right now and hopefully also in future only can be extended from each other but can not be mixed into one another. If at all, then most probably Lightweight Traits do make it into the specification one day.
Its architectural approach is specific to JavaScript. It has been mentioned quite often in the last few years ... esdiscuss.org: »about lightweight traits«, github.com/WebReflection: »features :: with«, webreflection.blogspot.com: »A future friendly, backward compatible, class utility«, reddit.com/r/javascript: »Functional Mixins in ECMAScript 2015«, raganwald.com: »Functional Mixins in ECMAScript 2015« ... and possibly is best compared to Angus Croll's Flight Mixins.
Pure function based Mixin/Trait approaches ... This is not an essay about 'Traits in Javascript', The many »Talents« of JavaScript ... do come closest to what the OP has ask for unless something similar to ...
...
...
...
... was shipped to ECMAScript land.
There is a really nice pattern with ES2015 classes (which I don't necessarily endorse as such) to create mixins by Sebastian Markbage, which I've slightly adapted.
The utility function
mixinClasses
can be used to mix in class factories (aka factory functions that return classes) into your new class:Which can be used as follows, for example with the two factory functions
Foo
andBar
: