johnny-dreamguns
10/7/2019 - 9:24 AM

Singleton

// SINGLETON

const mySingleton = (function(){
  let taskRepo;

  const createRepo = () => {
    const taskRepo = new Object('task');
    return taskRepo;
  }

  return {
    getInstance: () => {
      if(!taskRepo){
        taskRepo = createRepo();
      }
      return taskRepo;
    }
  };
})();

const s1 = mySingleton.getInstance();
const s2 = mySingleton.getInstance();

if(s1 === s2){
  console.log('same');
}