How to do it...

  1. Open your command line application and navigate to your workspace.
  2. Create a new folder named 08-06-using-composition-instead-of-inherritence.
  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 a new class named Rocket. Add a constructor that takes a constructor argument name and assigns it to an instance property. Then, define a simple print method:
// main.js 
class Rocket { 
  constructor(name) { 
    this.name = name; 
  } 
 
  print() { 
    console.log(this.name + ' is a Rocket'); 
  } 
}  
  1. Create a class named InactiveRocket that extends the Rocket class and assigns an additional lastFlow property in the constructor. Then, override the print method:
// main.js class InactiveRocket ...

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.