“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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。