本文實例講述了JS模式之單例模式基本用法。分享給大家供大家參考。具體如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 //singleton var SingletonTester = (function(){ function Singleton(options){ options = options || {}; this.name = "SingletonTester"; this.pointX = options.pointX || 6; this.pointY = options.pointY || 10; }; var instance; var _static = { name : "SingletonTester", getInstance : function(options){ if(instance === undefined){ instance = new Singleton(options) }; return instance; } }; return _static; })();希望本文所述對大家的javascript程序設計有所幫助。