Node.js Stream writable.writableObjectMode 属性
Stream 模块中的writable.writableObjectMode 属性用于获取 Writable 流的对象模式值。
句法:
writable.writableObjectMode
返回值:如果objectMode设置为true则返回true,否则返回false。
下面的例子说明了 Node.js 中writable.writableObjectMode 属性的使用:
示例 1:
// Node.js program to demonstrate the
// writable.writableObjectMode Property
// Accessing stream module
const stream = require('stream');
// Creating a stream and creating
// a write function
const writable = new stream.Writable({
// setting value of objectMode
objectMode: true,
// 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('GfG')
// Calling writable.writableObjectMode
// Property
writable.writableObjectMode;
输出:
GfG
true
示例 2:
// Node.js program to demonstrate the
// writable.writableObjectMode 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('GfG')
// Calling writable.writableObjectMode
// Property
writable.writableObjectMode;
输出
GfG
false
这里没有设置objectMode ,默认设置为false。
参考: https://nodejs.org/api/stream.html#stream_writable_writableobjectmode