We've seen how to create Symbol
that can be accessed globally, and those that can't be accessed outside of their initial context. Now, we'll see how to use them to create something that wasn't really possible in earlier versions of JavaScript.
In this recipe, we'll use local Symbol
to simulate a type that is available in many other languages, enums.
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, refer to the first two chapters.
- Open your command-line application and navigate to your workspace.
- Create a new folder named
11-03-symbols-simulate-enums
. - Create a
main.js
file that defines a newobject
namedLaunchSite
, property values of the object should be localSymbols
:
// main.js const LaunchSite = { KENNEDY_SPACE_CENTER: Symbol('Kennedy Space Center'), WHITE_SANDS: Symbol('White Sands Missile Range'), BAIKONUR: Symbol('Baikonur Cosmodrome'), ...