Browser-friendly inheritance fully compatible with standard node.js inherits()
npx @tessl/cli install tessl/npm-inherits@2.0.0Browser-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.
npm install inheritsCommonJS:
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.
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); // trueThe inherits package uses environment detection to provide the most appropriate inheritance implementation:
util.inherits function when availableObject.create() supportThe package exports a single function that establishes prototype-based inheritance between constructor functions, following the same patterns as Node.js's standard library.
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:
ctor.super_ property to reference the parent constructor (allows Child.super_ access)instanceof behavior for both child and parent typesParameters:
ctor (Function): The child constructor function that will inherit from the parentsuperCtor (Function|null): The parent constructor function to inherit from, or null to create an orphaned constructorReturn Value:
undefined - this function modifies the constructor functions in placeUsage 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 methodEnvironment Compatibility:
The function behavior is identical across environments:
util.inherits when availableObject.create() with property descriptorsPost-Inheritance Properties:
After calling inherits(Child, Parent):
Child.super_ references Parent constructorChild.prototype inherits from Parent.prototypenew Child() instanceof Parent returns truenew Child() instanceof Child returns trueChild.prototype.constructor equals Child