How to do it...

  1. Open your command-line application and navigate to your workspace.
  2. Create a new folder named 07-07-static-methods-on-all-instances.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  1. Create a main.js file with an empty object rocketMap and a class Rocket:
//main.jslet rocketMap = {};  
 
class Rocket {} 
  1. Create a static method named find that looks up rockets by string on the Rocket class:
class Rocket { 
  // ... 
  static find (name) {    return rocketMap[name];  } 
} 
  1. Add a construction that assigns a name property, and assigns the instance to the rocketMap:
class Rocket { 
  // ... 
  constructor(name) {    this.name = name;    rocketMap[name] = this;  } 
} 
  1. Create a main function that compares created instances ...

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.