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 a main function from main.js.
Create a main.js file that defines three new Rocket classes:
// main.js
class Rocket {}
class ActiveRocket extends Rocket {}
class OrbitingRocket extends ActiveRocket {}
Create a function listInheritance that takes an instance and uses Object.getPrototypeOf to get the names of all the classes until you reach the null type:
// main.js
function listInheritance (instance) {
const hierarchy = [];
let currClass = instance.constructor;
while (currClass.name) {
hierarchy.push(currClass.name);
currClass = Object.getPrototypeOf(currClass)
}
console.log(hierarchy.join(' -> '));
}
Create a main function that creates an instance of OrbitingRocket and lists its inheritance:
// main.js
export function main() {
const orbitingRocket = new OrbitingRocket();
listInheritance(orbitingRocket);
}
Start your Python web server and open the following link in your browser: http://localhost:8000/.