Library File and Meta-Class Enumeration

Let’s take a look at the Enumeration.js library file.

Add the library file Enumeration.js

The folder structure of our EnumerationApp extends the structure of the validation application by adding the Enumeration.js file in the lib folder. Because of this, we get the following folder structure with four files in the lib folder:

Thus, we get the following folder structure with four files in the lib folder:

EnumerationApp
 css
 lib
   browserShims.js
   errorTypes.js
   util.js
   Enumeration.js
 src
 index.html

In the Enumeration.js file, discussed in the next lesson, we define a meta-class Enumeration for creating enumerations as instances of this meta-class with the help of statements like GenderEL = new Enumeration(["male", "female", "other"]).

The meta-class enumeration

We define an Enumeration meta-class, which supports both simple enumerations and code lists, but this doesn’t record enumerations. A simple enumeration is defined by a list of labels in the form of a JS array as the constructor argument so that the labels are used for the names of the enumeration literals. On the other hand, a code list is defined as a special kind of key-value map in the form of a JS object as the constructor argument. This way, the codes are used for the names of the enumeration literals.

Consequently, the constructor needs to test whether the invocation argument is a JS array or not.

The first part of the code below shows how simple enumerations are created:

Get hands-on with 1200+ tech skills courses.