Skip to content

Instantly share code, notes, and snippets.

@Kolenov
Created September 5, 2017 16:44
Show Gist options
  • Save Kolenov/39361102e20560d2c9b8998475ed0619 to your computer and use it in GitHub Desktop.
Save Kolenov/39361102e20560d2c9b8998475ed0619 to your computer and use it in GitHub Desktop.
Singleton
var Singleton = (function () {
var instance;
function createInstance() {
var object = new Object("I am the instance");
return object;
}
return {
getInstance: function () {
// creates instans if it doesn't exist
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
// Usage:
// Creates new instance
var instance1 = Singleton.getInstance();
var instance2 = Singleton.getInstance();
// Outputs: true
console.log(instance1 === instance2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment