Skip to content

Instantly share code, notes, and snippets.

@chiro-hiro
Created March 9, 2020 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiro-hiro/891118c4613427d9c1fce283699adac8 to your computer and use it in GitHub Desktop.
Save chiro-hiro/891118c4613427d9c1fce283699adac8 to your computer and use it in GitHub Desktop.
class Foo {
constructor() {
this.constructedAt = (new Date()).toISOString();
}
static getInstance() {
if (typeof this.instance === 'undefined') {
this.instance = new Foo();
}
return this.instance;
}
getValue() {
return this.constructedAt;
}
}
setTimeout(function () {
const foo1 = Foo.getInstance();
console.log(foo1);
}, 1000);
setTimeout(function () {
const foo2 = Foo.getInstance();
console.log(foo2);
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment