Skip to content

Instantly share code, notes, and snippets.

@itsmunim
Last active March 1, 2020 12:38
Show Gist options
  • Select an option

  • Save itsmunim/496728b50b7bbba68a5e0e4244604a18 to your computer and use it in GitHub Desktop.

Select an option

Save itsmunim/496728b50b7bbba68a5e0e4244604a18 to your computer and use it in GitHub Desktop.
class SwimMixin {
// prevents duplication of implementation;
// can be a helper/util class as well
swim(context) {
// swim implementation
}
}
class Animal {
walk() {
this.throwNotImplemented();
}
fly() {
this.throwNotImplemented();
}
swim() {
this.throwNotImplemented();
}
throwNotImplemented() {
throw new Error('Not implemented');
}
}
class Fish extends Animal {
swim() {
swimMixinInstance.swim(this);
}
}
const fish = new Fish();
fish.swim(); // the fish swims
fish.fly(); // sorry not implemented in this kind of object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment