最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it impossible to tell if a function is a generator function if .bind() has been called on it? - Stack Overflow

programmeradmin2浏览0评论

Looks like calling .bind(this) on any generator function breaks my ability to see if the function is a generator. Any ideas on how to fix this?

var isGenerator = function(fn) {
    if(!fn) {
        return false;
    }

    var isGenerator = false;

    // Faster method first
    // Calling .bind(this) causes fn.constructor.name to be 'Function'
    if(fn.constructor.name === 'GeneratorFunction') {
        isGenerator = true;
    }
    // Slower method second
    // Calling .bind(this) causes this test to fail
    else if(/^function\s*\*/.test(fn.toString())) {
        isGenerator = true;
    }

    return isGenerator;
}

var myGenerator = function*() {
}

var myBoundGenerator = myGenerator.bind(this);

isGenerator(myBoundGenerator); // false, should be true

Looks like calling .bind(this) on any generator function breaks my ability to see if the function is a generator. Any ideas on how to fix this?

var isGenerator = function(fn) {
    if(!fn) {
        return false;
    }

    var isGenerator = false;

    // Faster method first
    // Calling .bind(this) causes fn.constructor.name to be 'Function'
    if(fn.constructor.name === 'GeneratorFunction') {
        isGenerator = true;
    }
    // Slower method second
    // Calling .bind(this) causes this test to fail
    else if(/^function\s*\*/.test(fn.toString())) {
        isGenerator = true;
    }

    return isGenerator;
}

var myGenerator = function*() {
}

var myBoundGenerator = myGenerator.bind(this);

isGenerator(myBoundGenerator); // false, should be true
Share Improve this question edited Nov 6, 2014 at 1:53 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Nov 6, 2014 at 0:11 Kirk OuimetKirk Ouimet 28.4k44 gold badges130 silver badges183 bronze badges 4
  • 2 bind() returns a new function. a plain function, with no ties to the past – dandavis Commented Nov 6, 2014 at 0:13
  • 1 Maybe this would be of interest to see how they do it: npmjs/package/generator-bind – jfriend00 Commented Nov 6, 2014 at 0:45
  • possible duplicate of check if function is a generator – user663031 Commented Nov 6, 2014 at 2:12
  • 1 @Kirk - I turned my ment into an answer. – jfriend00 Commented Nov 6, 2014 at 2:49
Add a ment  | 

3 Answers 3

Reset to default 6

Since .bind() returns a new (stub) function that only just calls the original with .apply() in order to attach the proper this value, it is obviously no longer your generator and that is the source of your issue.

There is a solution in this node module: https://www.npmjs/package/generator-bind.

You can either use that module as is or see how they solve it (basically they make the new function that .bind() returns also be a generator).

Yes, it is possible to tell if a function is a generator even if .bind() has been called on it:

function testIsGen(f) {
  return Object.getPrototypeOf(f) === Object.getPrototypeOf(function*() {});
}

This package has the solution:

https://www.npmjs/package/generator-bind

Basically, in order to get it to work you either need to polyfill Function.prototype.bind or call a custom bind() method.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论