writable.destroyed屬性是Stream模塊的內置應用程序編程接口,用於檢查writable.destroy()方法是否被調用。
用法:
writable.destroyed
返回值:如果在調用writable.destroy()方法之前,此屬性返回true,否則它返回false。
以下示例說明了Node.js中writable.destroyed屬性的用法:
範例1:
// Node.js program to demonstrate the
// writable.distroyed Property
// Accessing stream module
const stream = require('stream');
// Creating a stream and creating
// a write function
const writable = new stream.Writable({
// Write function with its
// parameters
write:function(chunk, encoding, next) {
// Converting the chunk of
// data to string
console.log(chunk.toString());
next();
}
});
// Writing data
writable.write('hi');
// Again writing some data
writable.write('hello');
// Calling destroy function
writable.destroy();
// Calling the Property
writable.destroyed;
輸出:
hi hello true
範例2:
// Node.js program to demonstrate the
// writable.distroyed Property
// Accessing stream module
const stream = require('stream');
// Creating a stream and creating
// a write function
const writable = new stream.Writable({
// Write function with its
// parameters
write:function(chunk, encoding, next) {
// Converting the chunk of
// data to string
console.log(chunk.toString());
next();
}
});
// Writing data
writable.write('hi');
// Again writing some data
writable.write('hello');
// Calling the Property
writable.destroyed;
輸出:
hi hello false
在上麵的示例中,可寫的輸出為false。調用屬性writable.destroyed之前未調用destroy()方法。
參考: https://nodejs.org/api/stream.html#stream_writable_destroyed
相關用法
- node.js Stream writable.writableLength用法及代碼示例
- node.js Stream writable.writableEnded用法及代碼示例
- node.js Stream readable.destroyed用法及代碼示例
- node.js Stream writable.writableCorked用法及代碼示例
- node.js Stream writable.writableHighWaterMark用法及代碼示例
- node.js Stream writable.writableFinished用法及代碼示例
- node.js Stream writable.writableObjectMode用法及代碼示例
- node.js Stream readable.readable用法及代碼示例
- node.js Stream writable.writable用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream writable.destroyed Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。