Skip to content

Instantly share code, notes, and snippets.

@poozipotti
Created February 7, 2024 17:35
Show Gist options
  • Save poozipotti/66c07e69d26848dc526c1dd60eca5cb1 to your computer and use it in GitHub Desktop.
Save poozipotti/66c07e69d26848dc526c1dd60eca5cb1 to your computer and use it in GitHub Desktop.
example of using static classes for singleton
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