karlakmkj

Factory functions

Dec 16th, 2020 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //function to create multiple object instances
  2. const robotFactory = (model, mobile) => {
  3.   return{
  4.     model: model,   //key name to be of the same name as the parameter
  5.     mobile,         //for shorthand, can leave out the parameter name since it's the same as the key name
  6.     beep(){
  7.       console.log('Beep Boop');
  8.     }
  9.   }
  10. }
  11.  
  12. //create object out of factory function
  13. const tinCan = robotFactory('P-500', true);
  14.  
  15. tinCan.beep();
Add Comment
Please, Sign In to add comment