How to tell function and generator function apart?

Domenic Denicola d at domenic.me
Tue Mar 3 22:26:18 UTC 2015


As discussed previously such overloading is pretty bad. If someone creates a generator-returning function manually, instead of using a generator function to do so, they will start getting unexpected results from your library. You might think this is rare but it's actually pretty reasonable---consider e.g. `yourLibrary(transform(function* () { ... }))` where `transform` is a higher-order function which manually creates a generator based on its argument.

An analogy would be that your and Dean's functions are similar to something like

```js
function getFullName(person) {
  if (person.constructor !== Person) {
    // Do something else, e.g. `return person.toString()`
  }

  return `${person.firstName} ${person.lastName}`;
}
```

That is, now you have prohibited people from using { firstName, lastName } object literals, Person subclass instances, Dog instances, etc. That's very unfortunate practice. The same goes for generators.



More information about the es-discuss mailing list