Node.js util.inherits() 方法
“util”模块提供了用于调试目的的“utility”函数。为了访问这些函数,我们需要调用它们(通过 'require('util')')。
util.inherits() (在 v0.3.0 中添加)方法是 util 模块的内置应用程序编程接口,其中构造函数从一个继承原型方法,并将该构造函数的原型设置为一个新对象,该对象是从超级构造器创建。它主要用于在Object.setPrototypeOf(constructor.prototype, superConstructor.prototype)之上添加或继承一些输入验证。并且 superConstructor 可以通过constructor.super_property来访问以获得额外的便利。它(即util.inherits() )的使用不太鼓励,而是建议使用 ES6 class 和 extends 关键字以获得语言级别的继承支持。
句法:
const util = require('util');
util.inherits(constructor, superConstructor)
参数:该函数接受上面提到的两个参数,如下所述:
构造函数< 函数> :是用户希望从类继承的任何< 函数>。
superConstructor < 函数> :它是任何< 函数>,主要用于添加或继承一些输入验证。
返回值<未定义>:返回未定义的值。
示例 1:文件名: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 EventEmitter function streamData() { emitEvent.call(this); } // Trying to print value console.log("1.> Returning util.inherits():", util.inherits(streamData, emitEvent)); // Returns undefined // Inheriting library via constructor console.log("2.>", streamData); // Whole library of the constructor console.log("3.>", emitEvent); // Inheriting from EventEmitter util.inherits(streamData, emitEvent); // Emiting events streamData.prototype.write = function(responseData) { this.emit('send_data', responseData); }; // Creating new stream by calling function const stream = new streamData('default'); // Printing instance of eventemitter console.log("4.> Instance of EventEmitter", stream instanceof emitEvent); // Returns true // Comparing value and type of an // instance with eventEmitter console.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 console stream.write('Finally it started!'); // Finally Received the data
使用以下命令运行index.js文件:
node index.js
输出:
1.> Returning util.inherits(): undefined
2.> [Function: streamData]
3.> [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!”
示例 2:文件名: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 true console.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 console manageStream.write('Finally it started streaming with ES6');
使用以下命令运行index.js文件:
node index.js
输出:
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”
参考: https://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor