Singleton Class JavaScript
Sample code of singleton Class of JavaScript
by sendil J
JavaScript
var singletonClass = (function() {
var instance;
function createInstance() {
var object = new Object('I am an instance');
return object;
}
return {
getInstance: function() {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
function run() {
var oneInstance = singletonClass.getInstance();
var twoInstance = singletonClass.getInstance();
alert(oneInstance === twoInstance);
}
run();