Skip to content

Instantly share code, notes, and snippets.

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