Wygląda na to, że wywołanie .bind (this) na dowolnej funkcji generatora przerywa moją zdolność sprawdzenia, czy funkcja jest generatorem. Wszelkie pomysły, jak to naprawić?Czy nie można stwierdzić, czy funkcja jest funkcją generującą, jeśli wywołano na niej funkcję .bind()?
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
bind() zwraca nową funkcję. prosta funkcja, bez powiązań z przeszłością – dandavis
Może to być interesujące, aby zobaczyć, jak to robią: https://www.npmjs.org/package/generator-bind – jfriend00
możliwy duplikat [sprawdź, czy funkcja jest generatorem ] (http://stackoverflow.com/questions/16754956/check-if-function-is-a-generator) –