Search⌘ K
AI Features

Getters, Setters, and Checks in Model Code

Explore how to implement implicit getters and setters in JavaScript model classes and apply validation checks for enumeration attributes. Understand how to safeguard internal properties and validate single or multiple enumeration values to strengthen your model code's reliability and consistency.

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:

Javascript (babel-node)
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 ...