The “util” module provides ‘utility’ functions that are used for debugging purposes. For accessing those functions we need to call them (by ‘require(‘util’)‘).
The util.inherits() (Added in v0.3.0) method is an inbuilt application programming interface of the util module in which the constructor inherits the prototype methods from one to another and the prototype of that constructor is set to a new object which is created from superConstructor. It is mostly used for adding or inheriting some input validation on top of Object.setPrototypeOf(constructor.prototype, superConstructor.prototype). And superConstructor can be accessed through the constructor.super_property for additional convenience. Its (i.e, util.inherits()) usage is not much encouraged instead use of ES6 class and extends keywords is recommended in order to get language level inheritance support.
Syntax:
const util = require('util');
util.inherits(constructor, superConstructor)
Parameters: This function accepts two parameters as mentioned above and described below:
constructor <Function>: It is any <function> that the user wants to be inherited from the class.
superConstructor <Function>: It is any <function> which is mostly used for adding or inheriting some input validation.
Return Value <undefined>: It returns undefined value.
Example 1: Filename: index.js
// Node.js program to demonstrate the// util.inherits() method// Using require to access util moduleconst util = require('util');const emitEvent = require('events');// MyStream function calling EventEmitterfunctionstreamData() {emitEvent.call(this);}// Trying to print valueconsole.log("1.> Returning util.inherits():",util.inherits(streamData, emitEvent));// Returns undefined// Inheriting library via constructorconsole.log("2.>", streamData);// Whole library of the constructorconsole.log("3.>", emitEvent);// Inheriting from EventEmitterutil.inherits(streamData, emitEvent);// Emiting eventsstreamData.prototype.write =function(responseData) {this.emit('send_data', responseData);};// Creating new stream by calling functionconst stream =newstreamData('default');// Printing instance of eventemitterconsole.log("4.> Instance of EventEmitter",streaminstanceofemitEvent);// Returns true// Comparing value and type of an// instance with eventEmitterconsole.log("5.> '===' comparison of an "+"Instance with EventEmitter",streamData.super_ === emitEvent);// Returns truestream.on('send_data', (responseData) => {console.log("6.>",`Data Stream Received:"${responseData}"`);});// Writing on consolestream.write('Finally it started!');// Finally Received the dataRun index.js file using the following command:
node index.js
Output:
1.> Returning util.inherits(): undefined
2.> [Function: streamData]
3.> <ref *1> [Function: EventEmitter] {once: [Function: once], ….., listenerCount: [Function (anonymous)]}
4.> Instance of EventEmitter true
5.> ‘===’ comparison of an Instance with EventEmitter true
6.> Data Stream Received: “Finally it started!”
Example 2: Filename: index.js
// Node.js program to demonstrate the// util.inherits() method in ES6// Using require to access util moduleconst util = require('util');const { inspect } = require('util');const emitEvent = require('events');class streamData extends emitEvent {write(stream_data) {// Emitting the data streamthis.emit('stream_data', stream_data);}}const manageStream =newstreamData('default');console.log("1.>", inspect(manageStream,false, 0,true))console.log("2.>", streamData)// Returns trueconsole.log("3.>", streamData === manageStream)console.log("4.>", manageStream)manageStream.on('stream_data', (stream_data) => {// Prints the write statement after streamingconsole.log("5.>", `Data Stream Received:"${stream_data}"`);});// Write on consolemanageStream.write('Finally it started streaming with ES6');Run index.js file using the following command:
node index.js
Output:
1.> streamData { _events: [Object: null prototype] {}, ……………………, [Symbol(kCapture)]: false}
2.> [Function: streamData]
3.> false
4.> streamData {_events: [Object: null prototype] {}, ……………………, [Symbol(kCapture)]: false}
5.> Data Stream Received: “Finally it started streaming with ES6”
Reference: https://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor


