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 creates two instances and then calls their takeOff methods:
// main.js 
export function main() { 
  const saturnV = new Rocket('Saturn V'); 
  const falconHeavy = new Rocket('Falcon Heavy'); 
  saturnV.takeOff(500); 
  falconHeavy.takeOff(); 
}  
  1. Start your Python web server and open the following link in your browser: 
    http://localhost:8000/.
  2. You will see the following output:
..................Content has been hidden....................

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