Skip to content

Instantly share code, notes, and snippets.

@carlosvazquez
Created August 15, 2019 03:27
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 carlosvazquez/113af7568c7d09588f668847f2c6d161 to your computer and use it in GitHub Desktop.
Save carlosvazquez/113af7568c7d09588f668847f2c6d161 to your computer and use it in GitHub Desktop.
var mySingleton = (function() {
//Singleton constructor is private
function Singleton() {
var privateVar1 = "I'm a private var";
this.publicVar1 = "I'm a public var";
this.publicMethod1 = function() {
console.log("Private var: " + privateVar1 + " public var: " + this.publicVar1);
}
}
//private var to store the single instance
var singleInstance;
//Return the object providing getInstance method
return {
getInstance: function() {
if (!singleInstance) singleInstance = new Singleton();
return singleInstance;
}
}
})();
var myInstance = mySingleton.getInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment