0

Trying to implement singleton pattern in javascript following some tutorials. Just wondering if there is any other way to implement the same ?

var singleton = (function(){
              var getInstance; //private variable

              var createWidget = function(){
                         var todayDate = new Date(); //private
                         var addCSS = function(){
                            console.log('THis is my css function');
                         };
                         var getDropDownData = function(){
                            console.log('This is my getDropDownData function');
                         };
                         return {
                            getDropDownData : getDropDownData,
                            addCSS: addCSS 
                         };
              };

              return {
                    getInstance: function(){
                          if(!getInstance) {
                              getInstance = createWidget();
                          }
                          return getInstance;
                    }
              };
})();
var obj = singleton.getInstance();

Implementing it by running anonymous function at onLoad and assiging it to some variable. Can we implement it without running this function at onLoad ?

Vivek Pratap Singh
  • 9,326
  • 5
  • 21
  • 34
  • You can implement a singleton in many ways, if you want a more definitive answer then you would need to provide more details on exactly what it is you are trying to do. – James Aug 16 '14 at 08:30
  • Nothing much, I just want single object for my class. We are dynamically creating widget with this function.I am looking for best and simple way for it. – Vivek Pratap Singh Aug 16 '14 at 08:39
  • Is this possibly a duplicate to [this](http://stackoverflow.com/questions/1479319/simplest-cleanest-way-to-implement-singleton-in-javascript)? – masa Aug 16 '14 at 08:44
  • @masa they have also shown the way same which I did. – Vivek Pratap Singh Aug 16 '14 at 08:49
  • What do you mean by "*can we implement it without running this function at onLoad*"? Well, yes, however, you need to elaborate what you mean. – James Aug 16 '14 at 09:07
  • As new to design patterns, We follow philosphy like "Do it when need it". Write a function and call it. So looking around , which removes the onLoad call of this func. – Vivek Pratap Singh Aug 16 '14 at 09:11

1 Answers1

0

You could always write a function to abstract away the boilerplate for writing singletons. For example this is what I would do:

function singleton(prototype) {
    var instance = null;

    return {
        getInstance: function () {
            if (instance === null) {
                var Instance = prototype.init || function () {};
                Instance.prototype = prototype;
                instance = new Instance;
            } return instance;
        }
    };
}

Then you can use this function to create singletons as follows:

var Widget = singleton({
    init: function () {
        var todayDate = new Date; // private
    },
    addCSS: function () {
        console.log("This is my addCSS function.");
    },
    getDropDownData: function () {
        console.log("This is my getDropDownData function.");
    }
});

After that you use the singleton as you normally would:

var widget = Widget.getInstance();

Hope that helps.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299