Skip to content

Instantly share code, notes, and snippets.

@gokhandemirhan
Forked from wpsmith/singleton.js
Created February 16, 2016 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokhandemirhan/2c3771b3093ab8762ee1 to your computer and use it in GitHub Desktop.
Save gokhandemirhan/2c3771b3093ab8762ee1 to your computer and use it in GitHub Desktop.
JavaScript: Singleton JS Example
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