Peter Seliger (2013-04-15T03:56:50.000Z)
github at esdiscuss.org (2013-07-12T02:26:56.830Z)
Hi, maybe one should discuss terminology first. What has been rediscovered within the last decade every now and again by JavaScript programming individuals, this languages capability for "functional or function based mixins", might deserve a closer look, before running into what I would call a meta programming temptation trap. We already have everything we need in order to create modular collections of behavior and also in order to provide and apply them to objects. "Mixin" might be a Ruby influenced term and does not completely cover what can be achieved by functions/closures [call] and [apply]. I'd rather tend to use Perl 6 "Roles" as a generic term. The smallest thinkable Role was a function body that implements a single method in a stateless way. Thus being a "Trait" if one follows the findings of the "Software Composition Group" at Bern University [ http://scg.unibe.ch/research/traits]. Any implementation that gets injected mutable state or does create mutable state on its own in oder to solve its task(s) then, from my point of view, should be referred to as "Mixin". > It seems that Mixins Are Awesome and this can take most advantages from being a function and not only an object: > http://webreflection.blogspot.ie/2013/04/flight-mixins-are-awesome.html > > AFAIK, all interfaces described in W3C such EventTarget and others could be also covered by this proposal ... so ... what do you think ? Andrea, you are right, but all it needs in my opinion are a module pattern, a module library of your choice, a naming convention for your Trait/Mixin-module Implementation (adjectives/adverbes, no nouns, first uppercase letter?) and again [call] or [apply]. For your given example of W3C interfaces, [EventTarget] should be implemented and internally referred to as [EventTargetMixin], but the module should expose [EventTargetMixin] as [Observable]. - example gist for Observable [https://gist.github.com/petsel/5385218]. - example gist for a Trait [https://gist.github.com/petsel/5385163]. There is another point that makes pure straightforward functional Trait/Mixin composition unique in JavaScript - passing additional data at apply time. One could write e.g. implementations that at apply time can adapt the naming of their functional API without changing the underlying implementation itself. [Observable] from the above provided gist example enables configuration of the default API method names by an optionally passed config object making it possible to serve all those weird observable API dialects. ```js var obj_01 = {}; var obj_02 = {}; Observable.call(obj_01); /* does add the default api methods [addEventListener], [removeEventListener], [hasEventListener] and [dispatchEvent] */ Observable.call(obj_02, { addEventListener: "on", removeEventListener: "off", hasEventListener: "hasObserver", dispatchEvent: "trigger" }); /* obj_02 does feature the custom api names [on], [off], [hasObserver] and [trigger] - the underlying methods are the same as of obj_01 */ ``` With meta programming approaches this advantage, one gets for free now, might get lost or if implemented less understandable. I discarded every single approach I made within the last 5 years for Trait/Mixin libraries, that tried to be smarter than what the languages core already provides except the last one that just sticks to a module library.