Node.js util.inherits() Method

13 Aug, 2020
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.
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.
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 module const util = require('util'); const emitEvent = require('events');  // MyStream function calling EventEmitterfunction streamData() {  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 constructor  console.log("3.>", emitEvent);// Inheriting from EventEmitter util.inherits(streamData, emitEvent);  // Emiting eventsstreamData.prototype.write = function(responseData) {  this.emit('send_data', responseData);};  // Creating new stream by calling functionconst stream = new streamData('default');// Printing instance of eventemitterconsole.log("4.> Instance of EventEmitter",               stream instanceof emitEvent); // Returns true  // Comparing value and type of an // instance with eventEmitterconsole.log("5.> '===' comparison of an "          + "Instance with EventEmitter", streamData.super_ === emitEvent); // Returns true  stream.on('send_data', (responseData) => {  console.log("6.>",   `Data Stream Received: "${responseData}"`);});  // Writing on consolestream.write('Finally it started!'); // Finally Received the data
Run 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 module const util = require('util');const { inspect } = require('util');const emitEvent = require('events');  class streamData extends emitEvent {  write(stream_data) {        // Emitting the data stream    this.emit('stream_data', stream_data);  }}  const manageStream = new streamData('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 streaming  console.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
Node.js-util-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React