1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function Singleton() { this.data = "singleton"; }
Singleton.getInstance = (function () { var instance;
return function () { if (instance) { return instance; } else { instance = new Singleton(); return instance; } }; })();
var sa = Singleton.getInstance(); var sb = Singleton.getInstance(); console.log(sa === sb); console.log(sa.data);
|