How to do it...

  1. Open your command line application and navigate to your workspace.
  2. Create a new folder named 08-10-use-throw-to-simulate-abstract-class.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  1. Create a main.js file that defines a new class named Rocket. In the constructor, check the constructor of the instance, if it's Rocket, then throw an error:
// main.js 
class Rocket { 
  constructor (name) { 
    this.name = name; 
    if (this.constructor === Rocket) { 
      throw new Error('Abstract Class Should not be 
instantiated'); } } }
  1. Create two child classes of Rocket:
// main.js 
class ActiveRocket extends Rocket {} 
class InactiveRocket extends Rocket {} 
  1. Create a main function that creates instances of each class of rocket. Notice that the Rocket class can't be instantiated:
// main.js 
export function main() { 
  const saturnV = new InactiveRocket('Saturn V'); 
  console.log(saturnV.name, ' is a Rocket ', saturnV instanceof 
Rocket); const falconHeavy = new ActiveRocket('Falcon Heavy'); console.log(falconHeavy.name, ' is a Rocket ', falconHeavy
instanceof Rocket); // throws an error; new Rocket('Not going to make it!'); }
  1. Start your Python web server and open the following link in your browser:
    http://localhost:8000/.
  1. 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.220.155.243