The ad hoc Enum generation pattern using Proxy

Lukas GaucasLukas Gaucas
1 min read

Consider the following Proxy pattern with its respective aliases (you can add more aliases on demand):

With the pattern in mind we can now write some code, and at the same time avoid using quotes for String values, at least in most cases, as follows:

In the above paragraph, I said “…at least in most cases“, to explain that consider the edge case where the key starts with a number: syntactically it’s invalid expression in JavaScript, however there is a Verbose-ish workaround:

What are your though on this pattern, do you think it would be useful in your future endeavours ? - You can leave a comment below…

Stay tuned !

0
Subscribe to my newsletter

Read articles from Lukas Gaucas directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lukas Gaucas
Lukas Gaucas

Digital nomad