We've seen how to check for inheritance and identity of the instantiated class as a Boolean expression. We might want to also see the full ancestry of an instance at once. In this recipe, we'll see how to do just that.
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
- Open your command line application and navigate to your workspace.
- Create a new folder named
08-09-checking-class-inheritance
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file that defines three newRocket
classes:
// main.js class Rocket {} class ActiveRocket extends Rocket {} class OrbitingRocket extends ActiveRocket {}
- Create a function
listInheritance
that takes an instance and usesObject.getPrototypeOf
to get the names of all the classes until you reach the null type:
// main.js function...