2. Make a JavaScript Class Model

Using the information design model shown in Figure 6.5 above as the starting point, we make a JavaScript class model, essentially by decorating properties with a «get/set» stereotype, implying that they have implicit getters and setters, and by adding (class-level) check methods:

Figure 7.1. A JS class model for the object type Book

A JS class model for the object type Book

Notice that, for any multi-valued enumeration attribute (like someThings) we add a class-level check function for single values (like checkSomeThing) and another one for value sets (like checkSomeThings), both returning an object of type ConstraintViolation.

The implicit getters and setters implied by the «get/set» stereotype are a special feature of ES5, allowing to define methods for getting and setting the value of a property p while keeping the simple syntax of getting its value with v = o.p, and setting it with o.p = expr. They require to define another, internal, property (like _p) for storing the value of p because the name "p" does not refer to a normal property, but rather to a pair of get/set methods.

The most common reason for using implicit getters and setters is the need to always check constraints before setting a property. This is also the reason why we use them.