JavaScript
CHALLENGE
const wm = new WeakMap();
class Counter {
#id;
static #nextId = 0;
constructor() {
this.#id = Counter.#nextId++;
wm.set(this, { count: 0, id: this.#id });
}
inc() {
const data = wm.get(this);
data.count++;
return data.count;
}
}
const a = new Counter();
const b = new Counter();
a.inc(); a.inc();
b.inc();
console.log(`${a.inc()} ${b.inc()} ${wm.has(a)} ${wm.get(a).id} ${wm.get(b).id}`);
876 ·