Created
March 17, 2018 15:09
-
-
Save linx4200/5dbde011d883698e77bdec7a6a4c2031 to your computer and use it in GitHub Desktop.
【Factory】惰性单例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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