Modules and Namespaces
Config Namespace
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
modestarts asdev.- The lookup object has keys
dev,test, andprod. - Bracket lookup reads the value at
dev. hostbecomeslocalhost.- 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.