factory Functions

PHOTO EMBED

Thu Jul 21 2022 18:24:08 GMT+0000 (Coordinated Universal Time)

Saved by @cruz #javascript

const robotFactory = (model,mobile) => {
  return { 
    model: model,
    mobile: mobile, 
    beep() {
      console.log('Beep Boop');
    } 
  }
};

const tinCan = robotFactory('P-500',true);
tinCan.beep();

///


const monsterFactory = (name, age, energySource, catchPhrase) => {
  return { 
    name: name,
    age: age, 
    energySource: energySource,
    scare() {
      console.log(catchPhrase);
    } 
  }
};

const ghost = monsterFactory('Ghouly', 251, 'ectoplasm', 'BOO!');
ghost.scare(); // 'BOO!'

//
content_copyCOPY