Search⌘ K

Enumerations and Model Classes in Model Code

Explore how to implement enumeration attributes using JavaScript classes while ensuring property value validation through getters and setters. This lesson helps you understand how to build clearer, more maintainable model code with enumeration constraints in front-end applications.

Code the enumerations

Enumerations are coded in the following way with the help of the Enumeration meta-class:

Javascript (babel-node)
var PublicationFormEL = new Enumeration(
["hardcover", "paperback", "ePub", "PDF"]);
var BookCategoryEL = new Enumeration(
["novel", "biography", "textbook", "other"]);
var LanguageEL = new Enumeration({ "en": "English", "de": "German", "fr": "French", "es": "Spanish" });

Notice that LanguageEL defines a code list enumeration, while PublicationFormEL defines a simple enumeration.

Code the model class as JS class

It’s a good idea to ...