“util”模块提供用于调试目的的‘utility’函数。要访问这些函数,我们需要调用它们(通过“ require(‘util’)”)。
util.inherits()(在v0.3.0中添加)方法是util模块的内置应用程序编程接口,在该接口中,构造函数将原型方法从一个方法继承到另一个方法,并将该构造方法的原型设置为从中创建的新对象超级构造函数。它主要用于在Object.setPrototypeOf(constructor.prototype,superConstructor.prototype)之上添加或继承一些输入验证。可以通过构造函数访问superConstructor.super_property,以获取更多便利。不鼓励使用(即util.inherits()),而建议使用ES6类并扩展关键字以获取语言级别的继承支持。
用法:
const util = require('util'); util.inherits(constructor, superConstructor)
参数:此函数接受上述和以下描述的两个参数:
-
构造函数<函数>:用户要从类继承的任何<function>。
-
超级构造函数<函数>:它是任何<function>,主要用于添加或继承某些输入验证。
返回值<undefined>:返回未定义的值。
范例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.> <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!”
范例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
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js GM implode()用法及代码示例
- Node.js GM drawPolygon()用法及代码示例
- Node.js GM sharpen()用法及代码示例
- Node.js GM edge()用法及代码示例
- Node.js GM write()用法及代码示例
- Node.js GM channel()用法及代码示例
- Node.js GM roll()用法及代码示例
- Node.js GM whiteThreshold()用法及代码示例
- Node.js GM whitePoint()用法及代码示例
- Node.js GM drawEllipse()用法及代码示例
- Node.js GM threshold()用法及代码示例
- Node.js GM chop()用法及代码示例
- Node.js GM thumbnail()用法及代码示例
- Node.js GM paint()用法及代码示例
注:本文由纯净天空筛选整理自vikas_g大神的英文原创作品 Node.js util.inherits() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。