CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-inherits

Browser-friendly inheritance fully compatible with standard node.js inherits()

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

inherits

Browser-friendly inheritance fully compatible with standard node.js inherits(). This package provides a lightweight alternative to requiring the full util module in browser environments, automatically falling back to Node.js util.inherits when available or using a standalone browser implementation when needed.

Package Information

  • Package Name: inherits
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install inherits

Core Imports

CommonJS:

const inherits = require("inherits");

Note: This package uses CommonJS exports and does not provide native ESM support. For ESM projects, use dynamic imports or a bundler that handles CommonJS modules.

Basic Usage

const inherits = require("inherits");

// Define parent constructor
function Animal(name) {
  this.name = name;
}

Animal.prototype.speak = function() {
  console.log(`${this.name} makes a sound`);
};

// Define child constructor
function Dog(name, breed) {
  Animal.call(this, name); // Call parent constructor
  this.breed = breed;
}

// Set up inheritance
inherits(Dog, Animal);

// Add child-specific methods
Dog.prototype.speak = function() {
  console.log(`${this.name} barks`);
};

Dog.prototype.wagTail = function() {
  console.log(`${this.name} wags tail`);
};

// Create instance
const myDog = new Dog("Rex", "Golden Retriever");
myDog.speak(); // "Rex barks"
myDog.wagTail(); // "Rex wags tail"

console.log(myDog instanceof Dog); // true
console.log(myDog instanceof Animal); // true
console.log(Dog.super_ === Animal); // true

Architecture

The inherits package uses environment detection to provide the most appropriate inheritance implementation:

  • Node.js Environment: Uses the built-in util.inherits function when available
  • Browser Environment: Falls back to a standalone implementation that mirrors Node.js behavior
  • Legacy Browser Support: Provides compatibility for browsers without Object.create() support

The package exports a single function that establishes prototype-based inheritance between constructor functions, following the same patterns as Node.js's standard library.

Capabilities

Inheritance Function

Establishes prototype-based inheritance between constructor functions, making the child constructor inherit from the parent constructor.

/**
 * Establishes prototype-based inheritance between constructor functions
 * @param {Function} ctor - The constructor function that will inherit (child)
 * @param {Function} superCtor - The parent constructor function to inherit from
 * @returns {undefined} This function does not return a value
 */
function inherits(ctor, superCtor);

Behavior:

  • Sets ctor.super_ property to reference the parent constructor (allows Child.super_ access)
  • Establishes prototype chain so child instances inherit parent methods
  • Maintains proper instanceof behavior for both child and parent types
  • Preserves constructor property on the child prototype
  • Compatible across Node.js and browser environments

Parameters:

  • ctor (Function): The child constructor function that will inherit from the parent
  • superCtor (Function|null): The parent constructor function to inherit from, or null to create an orphaned constructor

Return Value:

  • Returns undefined - this function modifies the constructor functions in place

Usage Patterns:

// Basic inheritance setup
function Parent() {}
function Child() {}
inherits(Child, Parent);

// With null parent (creates orphaned constructor)
function Orphan() {}
inherits(Orphan, null);

// Typical OOP pattern
function EventEmitter() {}
EventEmitter.prototype.on = function(event, listener) { /* ... */ };

function MyClass() {
  EventEmitter.call(this); // Call parent constructor
}
inherits(MyClass, EventEmitter);

// Child now has access to parent methods
const instance = new MyClass();
instance.on('event', callback); // Inherited method

Environment Compatibility:

The function behavior is identical across environments:

  • Node.js: Uses util.inherits when available
  • Modern Browsers: Uses Object.create() with property descriptors
  • Legacy Browsers: Uses temporary constructor function pattern

Post-Inheritance Properties:

After calling inherits(Child, Parent):

  • Child.super_ references Parent constructor
  • Child.prototype inherits from Parent.prototype
  • new Child() instanceof Parent returns true
  • new Child() instanceof Child returns true
  • Child.prototype.constructor equals Child

docs

index.md

tile.json