How to do it...

  1. Open your command-line application and navigate to your workspace.
  2. Create a new folder named 11-06-create-class-to-wrap-map.
  1. Create a main.js file that defines a new class named Rocket that takes a constructor argument name and assigns it to an instance property:
// main.js 
class Rocket { 
  constructor(name) { 
    this.name = name; 
  } 
   } 
  1. Create a class named RocketSiteMap file that creates a new map and assigns it as an instance property in the constructor:
// main.js 
class RocketSiteMap { 
  constructor () { 
    this.map = new Map(); 
     }    
   } 
  1. Add the set method that checks the type of the key and value arguments. This method should throw an error if the argument types are incorrect, otherwise set the pair as an entry on the map:
// main.js 
class RocketSiteMap { 
  set (site, rocket) { 
    if (!(rocket instanceof Rocket)) { 
      throw new Error('Value of `RocketMap` must be of type 
`Rocket`'); } else if (typeof site !== 'symbol') { throw new Error('Key of `RocketMap` must be of type
`Symbol`'); } this.map.set(site, rocket); } }
  1. Add a get method that returns the entry for key from the map:
// main.js 
class RocketSiteMap { 
  get (key) { 
    return this.get(key); 
   } 
  1. Create an enum of various launch sites:
// main.js 
const LaunchSite = { 
  KENNEDY_SPACE_CENTER: Symbol('Kennedy Space Center'), 
  JUIQUAN: Symbol('Jiuquan Satellite Launch Center'), 
  WHITE_SANDS: Symbol('Jiuquan Satellite Launch Center'), 
  BAIKONUR: Symbol('Baikonur Cosmodrome') 
}  
  1. Create a main function. Attempt to set various key and value pairs to an instance of RocketMap:
// main.js 
export function main() { 
  const rocketSiteMap = new RocketSiteMap(); 
  rocketSiteMap.set(LaunchSite.KENNEDY_SPACE_CENTER, new Rocket('US: 
Saturn V')); rocketSiteMap.set(LaunchSite.WHITE_SANDS, new Rocket('US: Falcon
Heavy')); console.log(rocketSiteMap) try { rocketSiteMap.set(LaunchSite.KENNEDY_SPACE_CENTER, 'Buzz
Lightyear'); } catch (e) { console.error(e); } try { rocketSiteMap.set('Invalid Lanch Site', new Rocket('Long
March')); } catch (e) { console.error(e); } }
  1. Start your Python web server and open the following link in your browser:
    http://localhost:8000/.
  1. You should 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.224.1.15