Skip to content

Instantly share code, notes, and snippets.

@arokia
Last active February 24, 2017 11:55
Show Gist options
  • Select an option

  • Save arokia/989bee0c24d0fb802d9571e1edb5c040 to your computer and use it in GitHub Desktop.

Select an option

Save arokia/989bee0c24d0fb802d9571e1edb5c040 to your computer and use it in GitHub Desktop.
Javascript Singleton Pattern
var Singleton = (function () {
var instance;
function createInstance() {
var object = new Object("I am the instance");
return object;
}
return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
function run() {
var instance1 = Singleton.getInstance();
var instance2 = Singleton.getInstance();
alert("Same instance? " + (instance1 === instance2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment