Skip to content

Instantly share code, notes, and snippets.

@shx-dev
Last active May 30, 2018 19:54
Show Gist options
  • Save shx-dev/dee7f5aa4a57f4e02846b74db3cb05fc to your computer and use it in GitHub Desktop.
Save shx-dev/dee7f5aa4a57f4e02846b74db3cb05fc to your computer and use it in GitHub Desktop.
Artigos | Array.reduce  - O canivete suíço da programação funcional | Mixins
const mix = (baseClass) => ({
with(...mixins) {
return mixins.reduce((newClass, mixin) => mixin(newClass), baseClass);
}
});
const UIStateMixin = (baseClass) => {
return class extends baseClass {
constructor() {
super(...arguments);
this.disabled = false;
this.loading = false;
}
disable() {
this.disabled = true;
return this;
}
showLoadingIndicator() {
this.loading = true;
return this;
}
}
}
class Component {
constructor(tagName) {
this.tagName = tagName;
}
}
class Button extends mix(Component).with(UIStateMixin) {
constructor() {
super('ui-button');
}
}
const button = new Button();
button /* tagName : ui-button */
.disable() /* disabled : true */
.showLoadingIndicator(); /* loading : true */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment