How to do it...

  1. Open your command line application and navigate to your workspace.
  2. Create a new folder named 08-09-checking-class-inheritance.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  4. Create a main.js file that defines three new Rocket classes:
// main.js 
class Rocket {} 
class ActiveRocket extends Rocket {} 
class OrbitingRocket extends ActiveRocket {}  
  1. 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(' -> ')); 
} 
  1. 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); 
} 
  1. Start your Python web server and open the following link in your browser: http://localhost:8000/.
  2. You should see output like the following:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.91.25