...

/

Getters, Setters, and Checks in Model Code

Getters, Setters, and Checks in Model Code

Let’s learn how the getters, setters, and checks for the model are coded.

Code the implicit getters and setters

For each property, we define implicit getters and setters using the predefined JS keywords get and set, like this:

Press + to interact
class Book {
...
get isbn() {
return this._isbn;
}
set isbn(n) {
var validationResult = Book.checkIsbnAsId(n);
if (validationResult instanceof NoConstraintViolation) {
this._isbn = n;
} else {
throw validationResult;
}
}
...
}

Notice that the implicit getters and setters access the corresponding internal property, like _isbn. This approach is based on the assumption ...