Search⌘ K

Implement the Class Hierarchy: Model Classes

Explore how to implement a class hierarchy with model classes such as Person, Employee, and Author in plain JavaScript. Understand defining subtype relationships, retrieving instances from persistent storage across related tables, and saving data correctly. This lesson helps you build functional class models and manage application data effectively using inheritance techniques without frameworks.

The JS class model shown below can be directly coded to get the code of the model classesPerson, Employee, and Author as well as for the enumeration type EmployeeCategoryEL.

Define subtype relationships

We define the subtype relationships between Employee and Person, as well as between Author and Person, with extends. For instance, in src/m/Employee.js we define:

Javascript (babel-node)
EmployeeCategoryEL = new Enumeration(["Manager"]);
class Employee extends Person {
constructor({ personId, name, empNo, category, department }) {
super({ personId, name });
this.empNo = empNo;
if (category) this.category = category;
if (department) this.department = department;
}
...
}

Load the instances of the root class

When we retrieve the instances of a class hierarchy’s root class (in our ...