What are the various options available for this?
Singletons are the one's that can be initialized only once.
In the simplest possible terms here is a singleton for you.
var singleton = {
key: "value"
};
Another way
var Singleton = (function(){
var instance;
var SingletonClass = function(){
this.someProp = "someValue";
// all other stuff you want on your singleton instance;
};
return {
getInstance: function(){
if(!instance){
instance = new SingletonClass();
}
return instance;
}
}
})();
var aSingleton = Singleton.getInstance();
var bSingleton = Singleton.getInstance();
console.log(aSingleton.someProp); //someValue
console.log(bSingleton.someProp); //someValue
aSingleton.someProp = "someOtherValue";
console.log(a.someProp); //someOtherValue
console.log(b.someProp); //someOtherValue
In case you want to use new keyword for initialization
var Singleton = (function () {
var instance;
function SingletonClass() {
this.someProp = "someValue";
// all other stuff you want on your singleton instance;
if (instance) {
return instance;
}
instance = this;
}
return SingletonClass;
}());
var aSingleton = new Singleton();
var bSingleton = new Singleton();
console.log(aSingleton.someProp); //someValue
console.log(bSingleton.someProp); //someValue
aSingleton.someProp = "someOtherValue";
console.log(aSingleton.someProp); //someOtherValue
console.log(bSingleton.someProp); //someOtherValue
Hope it helps.
Using 'class' and 'new' and 'this' in javascript just make your life harder, my answer is object literal. {} ;)
Marco Alka
Software Engineer, Technical Consultant & Mentor
While I think that singletons are mostly anti-patterns, because you could just as well write normal functions in a scoped context (example at the bottom), here are some ways you can achieve a singleton:
// ES5 var Singleton = function() { if (Singleton._isInitialized) { throw new Error('Can only be initialized once!'); } Singleton._isInitialized = true; this.foo = 42; }; var s1 = new Singleton(); // OK var s2 = new Singleton(); // Error: Can only be initialized once!// ES6 const Singleton = class Singleton { constructor() { if (Singleton._isInitialized) { throw new Error('Can only be initialized once!'); } Singleton._isInitialized = true; } foo() { /* ... */ } }; const s1 = new Singleton(); // OK const s2 = new Singleton(); // Error: Can only be initialized once!// ES5 Node.JS var isInitialized = false; module.exports = function() { if (isInitialized ) { throw new Error('Can only be initialized once!'); } isInitialized = true; this.foo = 42; };// ES6 Module let isInitialized = false; export default class Singleton { constructor() { if (isInitialized ) { throw new Error('Can only be initialized once!'); } isInitialized = true; } foo() { /* ... */ } }// ESNext Module export default class Singleton { #isInitialized = false; constructor() { if (this.#isInitialized ) { throw new Error('Can only be initialized once!'); } this.#isInitialized = true; } foo() { /* ... */ } }// Scoped functions instead of singleton const s1 = (() => { const singleton = {}; let privateField = 'HELLO from private'; singleton.foo = () => { console.log(privateField); }; return singleton; })(); s1.foo(); // There can never be another instantiation, // because there is nothing to be instantiated