📜  Node.js Stream readable.readableObjectMode 属性

📅  最后修改于: 2022-05-13 01:56:24.791000             🧑  作者: Mango

Node.js Stream readable.readableObjectMode 属性

Readable Stream 中的 readable.readableObjectMode属性,用于检查流的objectMode

句法:

readable.readableObjectMode

返回值:返回布尔值。

下面的示例说明了 Node.js 中readable.readableObjectMode 属性的使用:

示例 1:

// Node.js program to demonstrate the     
// readable.readableObjectMode Property
   
// Accessing readable stream module
const Readable = require('readable-stream').Readable;
  
// Constructing readable stream and
// setting the objectMode value
const readable = Readable({objectMode: true});
  
// Defining ._read method
readable._read = () => {};
  
// Instructions for reading data
readable.on('readable', () => {
  let chunk;
  
  // Using while loop and calling
  // read method with parameter
  while (null !== (chunk = readable.read(4))) {
  
    // Displaying the chunk
    console.log(`read: ${chunk}`);
  }
});
  
// Calling readableObjectMode
// Property
readable.readableObjectMode;
console.log("objectMode is true!!");

输出:

objectMode is true!!

示例 2:

// Node.js program to demonstrate the     
// readable.readableObjectMode Property
   
// Accessing readable stream module
const Readable = require('readable-stream').Readable;
  
// Constructing readable stream and
// setting the objectMode value
const readable = Readable({objectMode: false});
  
// Defining ._read method
readable._read = () => {};
  
// Instructions for reading data
readable.on('readable', () => {
  let chunk;
  
  // Using while loop and calling
  // read method with parameter
  while (null !== (chunk = readable.read(4))) {
  
    // Displaying the chunk
    console.log(`read: ${chunk}`);
  }
});
  
// Calling readableObjectMode
// Property
readable.readableObjectMode;
console.log("objectMode is false!!");

输出:

objectMode is false!!

参考: https://nodejs.org/api/stream.html#stream_readable_readableobjectmode