JSFiddle - React, Tailwind, and code Playground
by karthick6891
HTML
<!-- singelton javascript -->
JavaScript
var SingletonClass = (function(){
function SingletonClass() {
//do stuff
}
var instance;
return {
getInstance: function(){
if (instance == null) {
instance = new SingletonClass();
// Hide the constructor so the returned objected can't be new'd...
instance.constructor = null;
}
return instance;
}
};
})();
var s1 = SingletonClass.getInstance();
var s2 = SingletonClass.getInstance();
if (s1 == s2) {
alert ("Good singleton");
}
// Throws error
var s3 = new SingletonClass();
if (s3 == s2) {
alert ("Bad singleton");
}