8000 Add example for Singleton with private static · HowProgrammingWorks/Singleton@5df731e · GitHub
[go: up one dir, main page]

Skip to content

Commit 5df731e

Browse files
committed
Add example for Singleton with private static
1 parent f3db7e3 commit 5df731e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

JavaScript/8-class.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
class Singleton {
4+
static #instance;
5+
6+
constructor() {
7+
const instance = Singleton.#instance;
8+
if (instance) return instance;
9+
Singleton.#instance = this;
10+
}
11+
}
12+
13+
// Usage
14+
15+
console.assert(new Singleton() === new Singleton());
16+
console.log('instances are equal');

0 commit comments

Comments
 (0)
0