【发布时间】:2014-04-17 18:41:33
【问题描述】:
在为完全采用的收益支持做准备时,我已经发现了一个看似不足的地方。
在 nodejs 0.11+ 中有没有办法检测一个函数是否是一个生成器?
【问题讨论】:
在为完全采用的收益支持做准备时,我已经发现了一个看似不足的地方。
在 nodejs 0.11+ 中有没有办法检测一个函数是否是一个生成器?
【问题讨论】:
我不喜欢这种方式:
var
// pull out regex for speed
genRegex = /^function[\s]*\*/,
detectGenerator = function(mth){
return (typeof mth == 'function') &&
genRegex.test(mth.toString());
};
function * foo (){};
function *bar (){};
function* baz (){};
function*qux (){};
function non (){};
console.log(detectGenerator(function (){}), detectGenerator(function(){})) // false, false
console.log(detectGenerator(function *(){}), detectGenerator(function* (){})) // true, true
console.log(detectGenerator(function * (){}), detectGenerator(function*(){})) // true, true
console.log(detectGenerator(foo), detectGenerator(bar)) // true, true
console.log(detectGenerator(baz), detectGenerator(qux)) // true, true
console.log(detectGenerator(non)) // false
但它有效。
如果您有更好的选择,请回复。
【讨论】: