Group configuration values by mode.

Config Namespace

config_namespace.js
const mode = "dev";
const host = {
  dev: "localhost",
  test: "staging",
  prod: "api",
}[mode];
const url = "https://" + host + ".example.com";

console.log("mode=" + mode);
console.log("url=" + url);

Follow the Mode

  1. mode starts as dev.
  2. The lookup object has keys dev, test, and prod.
  3. Bracket lookup reads the value at dev.
  4. host becomes localhost.
  5. The URL becomes https://localhost.example.com. | mode | host value | printed URL | | --- | --- | --- | | dev | localhost | https://localhost.example.com | | test | staging | https://staging.example.com | | prod | api | https://api.example.com |
config-namespace Namespace-style objects can group constants as well as functions. A mode key selects the value needed for the current run.

Exercise: config_namespace.js

Reproduce mode=dev and url=https://localhost.example.com, then use modes test and prod to predict the staging and api URLs.