Node.js – util.types.isGeneratorObject() 方法


util.types.isGeneratorObject() 方法檢查傳遞的值是不是內建的生成器物件。如果滿足以上條件,則返回 True,否則返回 False。如果使用轉換工具,返回值可能與原始原始碼不同。

語法

util.types.isGeneratorObject(value)

引數

  • value − 此輸入值接收所需引數的輸入,並檢查它是不是 Generator 物件。

它根據傳遞的輸入值返回 True 或 False。

示例 1

建立一個檔案 "isGeneratorObject.js" 並複製以下程式碼片段。建立檔案後,使用命令 "node isGeneratorObject.js" 執行此程式碼。

// util.types.isGeneratorObject() Demo Example

// Importing the util module
const util = require('util');

// Defining a generator function
function* generator() {
   yield 1;
   yield 2;
   yield 3;
}

const gen = generator();

// Passing the generator object
console.log("1." + util.types.isGeneratorObject(gen));

// Passing the generator function
console.log("2." + util.types.isGeneratorObject(generator()));

輸出

C:\home
ode>> node isFloat32Array.js 1.true 2.true

示例 2

// util.types.isGeneratorObject() Demo Example

// Importing the util module
const util = require('util');

// Defining a generator function
function* infinite() {
   let index = 0;

   while (true) {
      yield index++;
   }
}

const generator = infinite(); // "Generator { }"

console.log(generator.next().value); // 0
console.log(generator.next().value); // 1
console.log(generator.next().value); // 2

// Passing the generator object
console.log("1." + util.types.isGeneratorObject(generator));

// Passing the generator next-value function
console.log("2." + util.types.isGeneratorObject(generator.next()));

輸出

C:\home
ode>> node isFloat32Array.js 0 1 2 1.true 2.false

更新時間: 17-8-2021

66 次觀看

開啟你的 職業生涯

完成課程認證

開始學習
廣告
© . All rights reserved.