Created
April 18, 2016 13:25
-
-
Save adrian7/14b243bdfe21169524cc1faf8abca60c to your computer and use it in GitHub Desktop.
Javascript singleton
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 random = ( "My Private Random Int = " + parseInt(1000 * Math.random()) ); | |
var privateVar = "My Private Variable"; | |
function privateMethod () { | |
return ( "My privateMethod()\n" + privateVar ); | |
} | |
/** | |
* Public interface | |
*/ | |
return { | |
publicMethod1: function () { | |
return ( "My publicMethod1()\n" + privateMethod() + "\n" + random ); | |
}, | |
publicMethod2: function () { | |
return ( "My publicMethod2()\n" + this.publicMethod1() ); | |
} | |
}; | |
})(); | |
alert( Singleton.publicMethod2() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment