Skip to content

Instantly share code, notes, and snippets.

@gnepud
Created April 12, 2012 09:50
Show Gist options
  • Save gnepud/2365992 to your computer and use it in GitHub Desktop.
Save gnepud/2365992 to your computer and use it in GitHub Desktop.
javascript singleton
function singleton() {
var instance = (function() {
var privateVar;
function privateMethod () {
// ...
}
return { // public interface
publicMethod1: function () {
// private members can be accessed here
},
publicMethod2: function () {
// ...
}
};
})();
singleton = function () { // re-define the function for subsequent calls
return instance;
};
return singleton(); // call the new function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment