Skip to content

Instantly share code, notes, and snippets.

@myrtleTree33
Created January 25, 2020 07:30
Show Gist options
  • Save myrtleTree33/0cde12389ac88088cb949122ebe6295a to your computer and use it in GitHub Desktop.
Save myrtleTree33/0cde12389ac88088cb949122ebe6295a to your computer and use it in GitHub Desktop.
Mixin functional composition example
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