JSFiddle - React, Tailwind, and code Playground
Singleton
JavaScript
const SingletonClass = (() => {
let instance;
function Constructor() {
this.x = 5;
}
return {
getInstance: () => {
if (!instance) {
instance = new Constructor();
}
return instance;
}
}
})();
console.log(SingletonClass.getInstance());
console.log(SingletonClass.getInstance());