var Singleton = (function(){
var _instance;
function init() {
function privateMethod() {}
var privateVariable = "42";
var privateRandomX = Math.random();
return {
publicMethod: function() {},
publicProperty: "23",
getRandomX: function() { return privateRandomX; }
};
};
return {
getInstance: function() {
if (!_instance) {
instance = init();
}
return _instance;
}
};
})();
// version 2
// static property
function Singleton() {
if (typeof Singleton.instance === "object") {
return Singleton.instance;
}
// init here
Singleton.instance = this; // flaw: anyone can modify
return this;
}