Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created November 12, 2020 14:28
Show Gist options
  • Save luojiyin1987/669c88d1efc13b8051330579b06ad432 to your computer and use it in GitHub Desktop.
Save luojiyin1987/669c88d1efc13b8051330579b06ad432 to your computer and use it in GitHub Desktop.
单例
class Demo{
private static instance: Demo;
private constructor(public name: string) {}
static getInstance() {
if(!this.instance) {
this.instance = new Demo('dell llw');
}
return this.instance;
}
}
const demo1 = Demo.getInstance();
const demo2 = Demo.getInstance();
console.log(demo1.name);
console.log(demo2.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment