Created
August 28, 2017 06:02
-
-
Save edtoken/83cdfe9c1f9e6a995cd1e815213baaee to your computer and use it in GitHub Desktop.
This file contains 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
// // USING: | |
// // Module.js | |
// import Singleton from './Singleton'; | |
// export default Singleton(() => { | |
// // logic... | |
// return new YourClass.apply(this, arguments); | |
// }); | |
// | |
// // app.js | |
// import Module from './Module'; | |
// const obj = Module.inst(); | |
// | |
// // other.js | |
// import Module from './Module'; | |
// const obj = Module.inst(); | |
// Singleton.js | |
export const Singleton = (callBackObjectCreate) => { | |
return (function() { | |
let instance = undefined; | |
function createInstance() { | |
instance = callBackObjectCreate(); | |
} | |
return { | |
inst: function() { | |
if (!instance) { | |
createInstance(); | |
} | |
return instance; | |
} | |
}; | |
})(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment