Skip to curriculum
Jump to the editor

9.2. A singleton

Easy
Topics

This question is in

9. Prototypes and classes
10–20 min

Some objects should exist once per page: the app's settings, a connection to a socket, a cache that every module reads. If two modules each build their own, one changes the theme and the other never sees it. A singleton is a class that only ever has one instance, and hands that same instance to everyone who asks.

Write the class Settings in settings.js. Settings.getInstance() returns the one instance, and so does new Settings(). The instance stores values by key.

Signature

JAVASCRIPT
class Settings {
  static getInstance()   // the one instance
  get(key)               // the stored value, or undefined
  set(key, value)        // stores value; returns the instance
  has(key)               // true when key was set
}

Keys are strings. Values can be anything.

Requirements

  1. Settings.getInstance() returns a Settings object, and the same object on every call.
  2. new Settings() returns that same instance too, every time.
  3. set(key, value) stores the value and returns the instance; get(key) returns what was stored (the same object, not a copy); a second set replaces the value.
  4. A value set through one reference is seen through every other reference.
  5. new Settings() never clears or resets the values already stored.
  6. get returns undefined and has returns false for a key never set; has is true for a key set to undefined.
  7. Every string is an ordinary key: has("toString") and has("constructor") are false until they are set, and "__proto__" stores and returns a value like any other key.
  8. The instance has no own properties.
  9. Outside code cannot replace the instance: the class has no static property other than getInstance (besides the built-in length, name and prototype), and assigning Settings.instance changes nothing.
  10. getInstance still works when called on its own: const { getInstance } = Settings; getInstance().

Examples

JAVASCRIPT
const a = Settings.getInstance();
const b = new Settings();
a === b;                      // true

a.set("theme", "dark").set("lang", "en");
b.get("theme");               // "dark"
JAVASCRIPT
const s = Settings.getInstance();
s.has("toString");            // false
s.set("__proto__", 1);
s.get("__proto__");           // 1
s.get("missing");             // undefined

Environment

  • Plain JavaScript in a browser page; the tests use the global class Settings from settings.js.
  • One page has one instance, so all the tests share it: they do not reset it between tests, and resetting is not part of this problem.

Hints

  1. A constructor normally returns the new this. What does new give back when the constructor returns some other object?
  2. Where can the class keep its one instance so that no code outside the class body can read or overwrite it?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…