Last active
September 19, 2017 15:28
-
-
Save nire0510/54444c1af1b7ccd8f317 to your computer and use it in GitHub Desktop.
[Singleton Boilerplate] #javascript
This file contains 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 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