Created
January 25, 2020 07:30
-
-
Save myrtleTree33/0cde12389ac88088cb949122ebe6295a to your computer and use it in GitHub Desktop.
Mixin functional composition example
This file contains 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 compose = (...fns) => fns.reduceRight( | |
(prevFn, nextFn) => (...args) => nextFn(prevFn(...args)), | |
value => value | |
); | |
const EatMixin = Superclass => class extends Superclass { | |
eat(food) { | |
console.log(`eating ${food}`); | |
} | |
} | |
const BarkMixin = Superclass => class extends Superclass { | |
bark(word) { | |
console.log(`bark ${word}`); | |
} | |
} | |
class Animal { | |
} | |
const SuperDog = compose(EatMixin, BarkMixin)(Animal); | |
const dog = new SuperDog(); | |
dog.bark('hihi'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment