Build Better JavaScript Enums with Proxies

Ben Smith
Ben Smith
Sep 6, 2018 · 2 min read

When I need an enum object in JavaScript, I have been in the habit of using Object.freeze() to create a read-only object. While this works, and is a popular practice, frozen objects provide you only immutability.

What if you wanted to throw an exception when an unknown enum option was referenced? Every interaction with your enum is going to be reading an option from that enum. The “frozen…

Keep the story going. Sign up for an extra free read.

You've completed your member preview for this month, but when you sign up for a free Medium account, you get one more story.
Already have an account? Sign in

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade