Prototype Inheritance Utility

0.0.3 · abandoned · verified Sun Apr 19

The `component-inherit` package is a minimalist utility designed for establishing prototype inheritance in JavaScript. Released at a very early stage (current version 0.0.3), it was part of the now-defunct `component.io` ecosystem, as indicated by its `component install` instruction in the README. Its primary function, demonstrated through a simple `inherit(Child, Parent)` pattern, is to link prototypes, effectively achieving what `Child.prototype = Object.create(Parent.prototype)` or, more recently, `class Child extends Parent {}` accomplish natively in modern JavaScript. The package is effectively abandoned, given its very low version number, lack of updates, and reliance on an archaic module system (CommonJS) and installation method (component.io) that predates widespread npm adoption and native ES modules. It has no active release cadence and its utility has been entirely superseded by native language features.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `component-inherit` to establish a prototype chain between two constructor functions in a CommonJS environment.

const inherit = require('inherit');

// Define a base constructor function (the parent)
function Human(name) {
  this.name = name;
}
Human.prototype.greet = function() {
  return `Hello, my name is ${this.name}`;
};

// Define a derived constructor function (the child)
function Woman(name, age) {
  Human.call(this, name); // Call parent constructor for inherited properties
  this.age = age;
}

// Establish prototype chain using component-inherit
inherit(Woman, Human);

// Add Woman-specific methods to its prototype
Woman.prototype.identify = function() {
  return `${this.greet()} and I am ${this.age} years old.`;
};

// Example usage
const alice = new Woman('Alice', 30);
console.log(alice.greet());
console.log(alice.identify());

// Verify instanceof chain
console.log(alice instanceof Woman); // true
console.log(alice instanceof Human); // true
console.log(Object.getPrototypeOf(Woman.prototype) === Human.prototype); // true

view raw JSON →