Created
November 12, 2020 14:28
-
-
Save luojiyin1987/669c88d1efc13b8051330579b06ad432 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
| 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