當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


node.js Stream readable.readableObjectMode用法及代碼示例


Readable Stream中的可讀.visibleObjectMode屬性,用於檢查流的objectMode。

用法:

readable.readableObjectMode

返回值:它返回布爾值。


以下示例說明了Node.js中visible.visibleObjectMode屬性的使用:

範例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



相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream readable.readableObjectMode Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。