JS: Polymorphism
2026-02-21 02:51 Diff

Full access to materials

Sign up and get access to this and dozens of other courses

Sign up

Another interesting trick is dispatching by file name.

In some systems, it's common to have different files belonging to separate environments for configuration rather than one file. For example:

configs/ database.development.json database.production.json database.test.json

Somewhere in the source code must be a code block that selects which file to load. The code below uses dispatching by key:

It is not difficult to see that using a startup environment name. So we can create a suitable file name. Let us do that:

The code is much shorter and no longer needs to be changed when expanding.

Recommended programs