Created
February 7, 2024 17:35
-
-
Save poozipotti/66c07e69d26848dc526c1dd60eca5cb1 to your computer and use it in GitHub Desktop.
example of using static classes for singleton
This file contains 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 Counter { | |
static instance; | |
static get count() { | |
if (!Counter.instance) { | |
Counter.instance = { count: 0 }; | |
} | |
return Counter.instance?.count; | |
} | |
static set count(newCount) { | |
Counter.instance.count = newCount; | |
} | |
static increment() { | |
return ++Counter.count; | |
} | |
static decrement() { | |
return --Counter.count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment