2. The Philosophy and Features of mODELcLASSjs

The concept of a class is fundamental in object-oriented programming. Objects instantiate (or are classified by) a class. A class defines the properties and methods for the objetcs that instantiate it.

There is no explicit class concept in JavaScript. However, classes can be defined in two ways:

  1. In the form of a constructor function that allows to create new instances of the class with the help of the new operator. This is the classical approach recommended in the Mozilla JavaScript documents.

  2. In the form of a factory object that uses the predefined Object.create method for creating new instances of the class.

Since we normally need to define class hierarchies, and not just single classes, these two alternative approaches cannot be mixed within a class hierarchy, and we have to make a choice whenever we build an app. With mODELcLASSjs, you choose the second approach with the following benefits:

  1. Properties are declared (with a property label, a range and many other constraints)

  2. Supports object pools

  3. Supports multiple inheritance and multiple classification

These benefits come with a price: objects are created with lower performance, mainly due to the fact that Object.create is slower than new. On the other hand, for apps with lots of object creation and destruction, such as games and simulations, mODELcLASSjs provides object pools for avoiding performance problems due to garbage collection.

The properties and methods of the meta-class mODELcLASS are listed in the following class diagram:

Figure 22.1. The meta-class mODELcLASS

The meta-class mODELcLASS

Notice that in the class diagram, we use the type oBJECT for values representing those special JavaScript objects that instantiate a model class created with mODELcLASS. These objects have a pre-defined property type referencing the model class they instantiate as their direct type. The values of the pre-defined property instances are maps of oBJECTs representing the extension (or population) of a model class.