While switching from Mootools to EcmaScript Classes I need to add some pre-built functionalities to classes. Like events...
Mootools uses an Implements parameter in Class for this.
var Widget = new Class({
Implements: Events,
initialize: function(element){
// ...
},
complete: function(){
this.fireEvent('complete');
}
});
Above, Implements adds some methods to class.
There are lots of mixin way of doing this on the net. But in the end I feel confused. Why we do not simply extend an Events class but use a more complicated mixin application.
I am looking for something dry and simple to reuse. Like this one;
class BaseAnimal {
//...
}
/* Events Mixin */
var Events={
//...
}
/* Options Mixin */
var Options={
//...
}
/* Extend base and add mixins */
class Parrot extends myMixinFuction(BaseAnimal,Events,Options){
//...
}
/* No Base. Only add mixin */
class Cat extends myMixinFuction(Events){
//...
}
myMixinFuction function (...args){
//do something that adds mixins to base class.
//if no base create one empty class and add mixinis to it
//return that Class
}