Created
August 15, 2019 03:27
-
-
Save carlosvazquez/113af7568c7d09588f668847f2c6d161 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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