Using Symbol to create a local instance
Symbols on their own aren't particularly useful, but they are very useful as keys for other data structures. They are well suited as keys because it is possible to restrict the access to their values. There are two ways these comparisons can work. We can create local symbols, which are unique and can be recreated after initialization, and global symbols, which can be referenced by their constructor value.
In this recipe, we'll take a look at how to use Symbol
as a function to create local symbols. This means that each instance will be new, even if the same arguments are used.
Getting ready
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.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
11-01-local-symbols
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
.
- Create...