Basic usage of JS singleton pattern

  • 2020-06-22 23:45:56
  • OfStack

This paper illustrates the basic usage of singleton pattern of JS pattern. Share to everybody for everybody reference. The details are as follows:


//singleton
var SingletonTester = (function(){
  function Singleton(options){
    options = options || {};
    this.name = "SingletonTester";
    this.pointX = options.pointX || 6;
    this.pointY = options.pointY || 10;
  };
  
  var instance;
  var _static = {
    name : "SingletonTester",
    getInstance : function(options){
      if(instance === undefined){
        instance = new Singleton(options)
      };
      return instance;
    }
  };
  return _static;
})();

Hopefully, this article has been helpful in your javascript programming.


Related articles: