Skip to content

Instantly share code, notes, and snippets.

@theScottyJam
Last active October 25, 2021 14:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Article: Composition Alone Can't Replace Inheritance - original inheritance example
export class Monster {
constructor() {
if (this.constructor === Monster) {
throw new Error('This is an abstract class. Do not instantiate it directly.');
}
}
attack() {
playHurtAnimation()
}
die() {
userInventory.add(this.dropLoot())
removeFromUi(this)
}
dropLoot() {
throw new Error('This is an abstract function. It should be shadowed.')
}
}
export class Slime extends Monster {
dropLoot() {
return [new SlimeBall()]
}
}
export class Skeleton extends Monster {
dropLoot() {
return [new Bone()]
}
}
export class Clown extends Monster {
dropLoot() {
return [new Key()]
}
attack() {
super.attack()
if (itsTime()) {
showCutScene()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment