Last active
October 25, 2021 14:02
-
-
Save theScottyJam/6980ae2127e7248f4ebe7d506f8b52b4 to your computer and use it in GitHub Desktop.
Article: Composition Alone Can't Replace Inheritance - original inheritance example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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