Skip to content

Instantly share code, notes, and snippets.

@theScottyJam
Last active October 25, 2021 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theScottyJam/6980ae2127e7248f4ebe7d506f8b52b4 to your computer and use it in GitHub Desktop.
Save theScottyJam/6980ae2127e7248f4ebe7d506f8b52b4 to your computer and use it in GitHub Desktop.
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