Skip to content

Instantly share code, notes, and snippets.

@adrian7
Created April 18, 2016 13:25
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 adrian7/14b243bdfe21169524cc1faf8abca60c to your computer and use it in GitHub Desktop.
Save adrian7/14b243bdfe21169524cc1faf8abca60c to your computer and use it in GitHub Desktop.
Javascript singleton
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