Skip to content

Instantly share code, notes, and snippets.

@linx4200
Created March 17, 2018 15:09
Show Gist options
  • Save linx4200/5dbde011d883698e77bdec7a6a4c2031 to your computer and use it in GitHub Desktop.
Save linx4200/5dbde011d883698e77bdec7a6a4c2031 to your computer and use it in GitHub Desktop.
【Factory】惰性单例
var LazySingleton = (function() {
// 单例实例引用
var instance = null;
// 单例
function Singleton() {
return {
publicMethod: function() {},
publicProperty: '1'
}
}
return function () {
if (!instance) {
instance = Singleton();
}
return instance;
}
})();
console.log(LazySingleton().publicProperty); // '1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment