Node.js util.types.isGeneratorObject() 方法
util.types.isGeneratorObject() 方法是 util 模块的内置应用程序编程接口,主要用于支持 Node.js 自身内部 API 的需求。
util.types.isGeneratorObject() 方法用于检查给定值是否是生成器对象。
句法:
util.types.isGeneratorObject( value )
参数:此函数接受单个参数值,该值保存将检查生成器对象的值。
返回值:它返回一个布尔值,即如果传递的值是生成器对象,则返回 true,否则返回false 。
下面的程序说明了 Node.js 中的util.types.isGeneratorObject() 方法:
示例 1:
// Node.js program to demonstrate the
// util.types.isGeneratorObject() method
// Import the util module
const util = require('util');
// Creating a generator function
let GeneratorFunction =
Object.getPrototypeOf(function*(){}).constructor
let genFn = new GeneratorFunction();
// Checking the generator object
let genObj = genFn();
console.log(genObj);
isGenObj = util.types.isGeneratorObject(genObj);
console.log("Object is a generator object:", isGenObj);
// Checking a normal object
normalObj = {a: "1", b: "2"};
console.log(normalObj);
isGenObj = util.types.isGeneratorObject(normalObj);
console.log("Object is a generator object:", isGenObj);
输出:
Object [Generator] {}
Object is a generator object: true
{ a: '1', b: '2' }
Object is a generator object: false
示例 2:
// Node.js program to demonstrate the
// util.types.isGeneratorObject() method
// Import the util module
const util = require('util');
// Creating a generator function
let genFn = function* generateNumber() {
let id = 0;
while (true)
yield id++;
};
// Checking the generator object
let genObj = genFn();
console.log(genObj);
isGenObj = util.types.isGeneratorObject(genObj);
console.log("Object is a generator object:", isGenObj);
// Checking a normal object
normalObj = {arg1: "1", arg2: "2"};
console.log(normalObj);
isGenObj = util.types.isGeneratorObject(normalObj);
console.log("Object is a generator object:", isGenObj);
输出:
Object [Generator] {}
Object is a generator object: true
{ arg1: '1', arg2: '2' }
Object is a generator object: false
参考: https://nodejs.org/api/util.html#util_util_types_isgeneratorobject_value