Last active
May 30, 2018 19:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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