JS Singleton Class
JavaScript
var SingletonClass = (function() {
function SingletonClass() {
//do stuff
}
var instance;
var n;
return {
getInstance: function() {
if (instance == null) {
instance = new SingletonClass();
// Hide the constructor so the returned objected can't be new'd...
instance.constructor = null;
n = 0;
}
else n++;
alert("Value" + n);
return instance;
}
};
})();
var test = SingletonClass.getInstance();