mercredi 1 janvier 2020

Simple JS example of Singleton Pattern not working

I made a simple example of Singleton Pattern but it is giving blank objects in both cases. Please someone help?

var a = ( function(){
        
  var instance = {};

  function init() {
     return {
      name: "rosy",
      age: 29,
      printName: function(){
        console.log(this.name)
      }
    }
  }

  return {
    getInstance: function(){
      if(!instance) {instance = init();}
      return instance;
    }
  }
})();

console.log(a.getInstance());
console.log(a.getInstance());

Aucun commentaire:

Enregistrer un commentaire