Introduction to Subtyping in Plain JavaScript

Let’s have a quick introduction of the contents of the chapter.

We'll cover the following

Introduction

Whenever an application has to manage the data of a larger number of object types, there may be various subtype relationships between some of the object types. Handling subtype relationships is an advanced issue in software application engineering. It’s often not well supported by application development frameworks.

In this chapter, we’ll first explain the general approach to constructor-based subtyping in JavaScript, before we take a look at two case studies based on fragments of our current example’s information model, the Public Library Application.

In the first case study, we consider the single-level class hierarchy with the root Book, which is an incomplete disjoint rigid segmentation. We use the Class Hierarchy Merge design pattern to refactor this simple class hierarchy to a single class that can be mapped to a persistent database table.

In the second case study, we consider the multi-level class hierarchy that consists of the Person roles Employee, Manager, and Author. The segmentation of Person into Employee and Author doesn’t have any constraints, which means it’s incomplete, overlapping (non-disjoint), and non-rigid.

We use the Class Hierarchy Merge design pattern to refactoring the simple Manager-is-Employee sub-hierarchy. We also use the Joined Tables Inheritance approach to map the Employee-and-Author-is-a-Person class hierarchy to a set of three database tables that are related to each other via foreign key dependencies

In both case studies, we show:

  1. How to derive a JS class model and a corresponding entity table model from the information design model.
  2. How to code the JS class model in the form of JS model classes.
  3. How to write the view and controller code based on the model code.

Get hands-on with 1400+ tech skills courses.