Modelo.js Inheritance Utility
Modelo.js is a JavaScript utility providing a robust and performant mechanism for object inheritance, specifically designed to extend Node.js's `util.inherits` with multiple inheritance capabilities. As of version 4.2.3, it allows developers to inherit from multiple base objects seamlessly, addressing a common limitation in standard JavaScript inheritance patterns. A key feature is the `isInstance` method, which is automatically added to instances and serves as a reliable replacement for the native `instanceof` operator when working with multiple inheritance, as `instanceof` is inherently limited to single-prototype chains. The library emphasizes performance, aiming for overhead comparable to `util.inherits` despite its added complexity. While the release cadence isn't explicitly detailed, the version number suggests ongoing maintenance. It differentiates itself by offering a familiar Node.js-style interface while introducing powerful multiple inheritance features, making it suitable for projects requiring flexible object composition.
Common errors
-
TypeError: instance is not an instance of MixinTwo
cause Attempting to use the native `instanceof` operator to verify inheritance from a base class that is not the first prototype in a multiple inheritance chain.fixUse the `isInstance` method provided by Modelo.js on the instance itself: `instance.isInstance(MixinTwo)`. -
TypeError: Cannot read property 'method' of undefined (or similar error when calling super_ method)
cause Incorrectly attempting to use `this.constructor.super_.prototype.method.call(this)` in a class that inherits from multiple parents, expecting `super_` to resolve to the correct parent in all cases.fixFor multiple inheritance, directly invoke the method on the specific parent prototype: `MixinTwo.prototype.method.call(this, ...)` instead of relying on `super_`.
Warnings
- gotcha When using multiple inheritance with Modelo.js, the native JavaScript 'instanceof' operator will not correctly identify all base objects in the inheritance chain. It will only report true for the first prototype listed in the 'inherits' call.
- gotcha The `super_` attribute on constructors (e.g., `Combined.super_`), while present for compatibility with `util.inherits`, only references the first prototype when multiple inheritance is used. Relying on it for method calls in multi-inherited classes can lead to incorrect or incomplete behavior.
Install
-
npm install modelo -
yarn add modelo -
pnpm add modelo
Imports
- inherits
import { inherits } from 'modelo';const { inherits } = require('modelo'); - modelo (entire module)
import modelo from 'modelo';
const modelo = require('modelo');
Quickstart
const modelo = require('modelo');
function Base() {
// Base object constructor
}
Base.prototype.baseMethod = function baseMethod() {
console.log('Method from base object.');
};
function Extension() {
// Sub-object constructor
}
modelo.inherits(Extension, Base);
const extInstance = new Extension();
extInstance.baseMethod(); // Outputs: Method from base object.
function MixinOne() {}
MixinOne.prototype.methodOne = function() { console.log('From MixinOne'); };
function MixinTwo() {}
MixinTwo.prototype.methodTwo = function() { console.log('From MixinTwo'); };
function Combined() {}
modelo.inherits(Combined, MixinOne, MixinTwo);
const instance = new Combined();
instance.methodOne(); // Outputs: From MixinOne
instance.methodTwo(); // Outputs: From MixinTwo
console.log(instance.isInstance(Combined)); // true
console.log(instance.isInstance(MixinOne)); // true
console.log(instance.isInstance(MixinTwo)); // true