How to do it...

  1. Open your command-line application and navigate to your workspace.
  2. Create a new folder named 07-03-defining-methods.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  4. Create a main.js with a class named Rocket, which assigns a name property upon construction:
// main.js 
class Rocket { 
  constructor(name) { 
    this.name = name; 
  } 
} 
  1. Add a method named takeoff that accepts an option countdown argument. The body of the method should log a message before and after a timeout:
// main.js 
class Rocket { 
  // ... 
  takeOff(countdown = 1000) { 
    console.log(this.name + ' starting countdown.'); 
    setTimeout(() => { 
      console.log(`Blastoff! ${this.name} has taken off`); 
    }, countdown); 
  } 
} 
  1. Add a main function that ...

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.