Node.js Reference ยท util, assert and the Test Runner
Node.js util.inherits() function
Reference for util.inherits() in Node.js: what it does to the prototype chain, how super_ works, why it is legacy, and the class extends syntax to use instead.
util.inherits(constructor, superConstructor) makes one constructor function inherit from another by linking their prototypes. It was the standard way to build subclasses in Node before ES2015 classes, particularly to extend EventEmitter and streams.
It is legacy. The Node.js documentation recommends class ... extends instead, and nothing about the function will be improved. It is worth knowing because older code and older tutorials use it constantly.
import { inherits } from "node:util"; import { EventEmitter } from "node:events"; function Door() { EventEmitter.call(this); } inherits(Door, EventEmitter);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
util.inherits(constructor: Function, superConstructor: Function): void
| Name | Description |
|---|---|
constructor |
The child constructor function. |
superConstructor |
The parent constructor function. |
Returns: undefined.
What it does: it sets constructor.super_ = superConstructor and links the prototype chain so that Object.getPrototypeOf(constructor.prototype) === superConstructor.prototype. In current Node versions it uses Object.setPrototypeOf() for the link.
Errors: throws ERR_INVALID_ARG_TYPE if either argument is not a function, or if superConstructor.prototype is not an object.
Usage notes
- Call
inherits()before adding methods toconstructor.prototypein older code styles; withsetPrototypeOfthe link is made in place, but keeping the call first is the convention and keeps your intent clear. - It does not call the parent constructor. Do that yourself inside the child with
Parent.call(this, ...); forgetting it leaves the parent's state (for example an emitter's listener table) uninitialized. super_is a plain property, not a language feature. Old code callsChild.super_.prototype.method.call(this); classes usesuper.method().- It only wires prototypes. It does not copy static properties from the parent and does not make the child a real
class; calling a class constructor withoutnewstill throws. - A
classcan extend a function constructor withextends, so you can modernize children one at a time. - Migrating: replace
function Child() { Parent.call(this); } inherits(Child, Parent)withclass Child extends Parent { constructor() { super(); } }and move prototype methods into the class body. - ES2015
Object.setPrototypeOf(Child.prototype, Parent.prototype)gives the same prototype link withoutsuper_.