Static & Private Class Fields
What benefit does using private fields provide in this implementation?
class Cache {
#data = new Map();
static #DEFAULT_TTL = 3600000;
set(key, value, ttl = Cache.#DEFAULT_TTL) {
this.#data.set(key, {
value,
expires: Date.now() + ttl
});
}
get(key) {
const entry = this.#data.get(key);
if (!entry) return null;
if (Date.now() > entry.expires) {
this.#data.delete(key);
return null;
}
return entry.value;
}
}