It depends on what you want to achieve, as different implementations will have different advantages and limitations.
The simplest implementation is just an object literal:
var singleton = { property: "foo", method: function() { alert('bar'); } }
The implementation you mentioned in the question allows public and private methods by encapsulating methods in closure and returning what needs to be revealed.
Here is an alternative that would similarly allow public and private methods and would be more extensible:
function MySingletonClass() { if ( arguments.callee._singletonInstance ) return arguments.callee._singletonInstance; arguments.callee._singletonInstance = this; this.Foo = function() {
source share