class Config {
#data = {};
static #instance;
set(k, v) { this.#data[k] = v; }
get(k) { return this.#data[k]; }
static getInstance() { return Config.#instance ?? (Config.#instance = new Config()); }
}Singletons are ideal for shared configuration stores accessed throughout the app.
Instead of using static properties, we can use a closure (IIFE) to create a private scope and ensure only one instance exists.
Challenge: Implement a closure-based singleton Config object. The getInstance() function should return a cached object that persists across calls.
Example:
const config1 = Config.getInstance()
const config2 = Config.getInstance()
config1 === config2 // true