Skip to content

Latest commit

 

History

History
82 lines (62 loc) · 1.06 KB

singleton.md

File metadata and controls

82 lines (62 loc) · 1.06 KB

Singleton

An object can have only one instance.

const Singleton = (function () {
    let instance;
    
    function createInstance() {
        const object = {a: 1, b: 2};
        return object;
    }
    
    return {
        getInstance() {
            if (!instance) {
                instance = createInstance();
            }
            
            return instance;
        }
    };
})();
var inst1 = Singleton.getInstance();
var inst2 = Singleton.getInstance();

inst1 === inst2
// true

Handling new

  • with function approach:
function Singleton() {
  this.a = 1;
  this.b = 2;

  if (!Singleton.inst) {
    Singleton.inst = this;
  }

  return Singleton.inst;
}
const inst1 = new Singleton();
const inst2 = new Singleton();

inst1 === inst2
// true
  • with class approach:
class Singleton {
  a = 1;
  b = 2;

  constructor() {
    if (!Singleton.inst) {
      Singleton.inst = this;
    }

    return Singleton.inst;
  }
}
const inst1 = new Singleton();
const inst2 = new Singleton();

inst1 === inst2
// true