-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiton.js
More file actions
29 lines (25 loc) · 835 Bytes
/
Copy pathMultiton.js
File metadata and controls
29 lines (25 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Multiton
{
static #id = -1;
static #countOfInstances = 10;
static #objects = [];
static #isInternalConstructing = false;
constructor() {
if (!Multiton.#isInternalConstructing) {
throw new TypeError("Multiton is not constructable");
}
}
static createObject()
{
Multiton.#id++;
if (Multiton.#id < Multiton.#countOfInstances) {
Multiton.#isInternalConstructing = true;
Multiton.#objects.push(new Multiton());
Multiton.#isInternalConstructing = false;
return Multiton.#objects[Multiton.#id];
}
const times = Math.floor(Multiton.#id / Multiton.#countOfInstances);
const index = Multiton.#id - (Multiton.#countOfInstances * times);
return Multiton.#objects[index];
}
}