我目前知道在
JavaScript中构建单例的两种方法.第一:
var singleton = { publicVariable: "I'm public",publicMethod: function() {} };
第二:
(function() { var privateVariable = "I'm private"; var privateFunction = function() {} return { publicVariable: "I'm public",publicMethod: function () {} } })();
解决方法
function Singleton() { if ( Singleton.instance ) return Singleton.instance; Singleton.instance = this; this.prop1 = 5; this.method = function() {}; }