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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP