Last active
September 18, 2017 09:48
-
-
Save thebiltheory/01788cba933ebb364d71ba0df3d868cb to your computer and use it in GitHub Desktop.
Singleton example
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
/** | |
* Singleton Pattern | |
* @returns {Object} | |
*/ | |
let Planet; | |
{ | |
let instance; | |
Planet = function () { | |
if (instance) { | |
return instance; | |
} | |
this.name = 'Sun'; | |
this.type = 'Star'; | |
} | |
instance = this; | |
}; | |
let sun = new Planet(); | |
let star = new Planet(); | |
console.log(sun === star); //True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment