Created
April 12, 2012 09:50
-
-
Save gnepud/2365992 to your computer and use it in GitHub Desktop.
javascript singleton
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
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