util.inherits
is a function of Node.js that is in the util
module.
In util.inherit
, the constructor inherits the prototype methods from one to another. The prototype of that constructor is placed in a new object created with superConstructor
.
The
util
module gives utility functions that can be used for debugging purposes.
To access the util.inherit
function, the util
module needs to be accessed, as shown below.
const util = require('util');
util.inherits(constructor, superConstructor)
The function takes in two parameters.
constructor
: The function that the programmer wants to be inherited from the class.
superConstructor
: A function that is primarily used for adding/inheriting input validation.
An undefined value is returned upon successful execution of the function.
The code below shows how the util.inherits
function operates.
Mystream
and emitEvent
are both passed as arguments to util.inherits
. MyStream
is a constructor
, whereas emitEvent
is a superConstructor
.
const util = require('util');const emitEvent = require('events');// MyStream function calls EventEmitterfunction MyStream() {emitEvent.call(this);}console.log("util.inherit returns :",util.inherits(MyStream, emitEvent));
Free Resources