-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path1-simple.js
More file actions
20 lines (15 loc) · 424 Bytes
/
Copy path1-simple.js
File metadata and controls
20 lines (15 loc) · 424 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'use strict';
function Singleton() {
const { instance } = Singleton;
if (instance) return instance;
Singleton.instance = this;
}
// Usage
console.assert(new Singleton() === new Singleton());
console.log('instances are equal');
// But instance is accessible
const a1 = new Singleton();
Singleton.instance = null;
console.log('Remove instance');
const a2 = new Singleton();
if (a1 !== a2) console.log('a1 !== a2');