Created
December 14, 2017 04:34
-
-
Save xfateless/f6a30290ef0be65f48264ec40d500217 to your computer and use it in GitHub Desktop.
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
function Person() { | |
if(typeof Person.instance === 'object') | |
return Person.instance; | |
Person.instance = this; | |
return this; | |
} | |
let person1 = new Person(); | |
let person2 = new Person(); | |
/* test to see whether we have access to the instance through the window */ | |
/* by comparing the window accessible instance to the reference variables */ | |
console.log(window.Person.instance === person2); //true | |
console.log(window.Person.instance === person1); //true | |
//references to the same instance, not just copies of the same object template | |
person1 === person2; //true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source: https://github.com/fbeline/Design-Patterns-JS/blob/master/src/creational/singleton/singleton.js