0 points

Javascript design patterns - Singleton pattern - Object - POO


dashboard

Wed Nov 04 2020 13:04:31 GMT+0000 (UTC)

Posted by @julien #javascript

const utils = (function () {
  let instance;
  
  function initialize() {
    return {
      sum: function (a, b) {
        return a + b;
      }
    };
  }
  return {
    getInstance: function () {
      if (!instance) {
        instance = initialize();
      }
      return instance;
    }
  };
})();
let sum = utils.getInstance().sum(3, 5); // 8
content_copy Copy

https://medium.com/javascript-in-plain-english/7-javascript-design-patterns-every-developer-should-know-df9c40e7debf