kefirjs / kefir

A Reactive Programming library for JavaScript
https://kefirjs.github.io/kefir/
MIT License
1.87k stars 97 forks source link

It is not possible to use .emit function as a parameter to some other high-order function. #65

Closed aindlq closed 9 years ago

aindlq commented 9 years ago

Lets say we have some simple high-order function and Emitter. And we want to use them together like:

function apply(f) {
  return function(x) {
    return f(x);
  }
}

var emitter = Kefir.emitter();
apply(emitter.emit)(1);

But it fails with exception:

Uncaught TypeError: Cannot read property '_send' of undefined kefir.js:2234(anonymous function) @ VM80:4(anonymous function) @ VM80:9InjectedScript._evaluateOn @ VM61:888InjectedScript._evaluateAndWrap @ VM61:821InjectedScript.evaluate @ VM61:687

The workaround is to bind this for emit, but this kind of annoying:

apply(emitter.emit.bind(emitter))(1);
rpominov commented 9 years ago

Most of methods in Kefir are not bound to their context to minimize memory footprint. So yes, you have to do .bind when passing it somewhere, but I don't see it as a workaround, it's just a normal thing you have to do in JavaScript when working with most of libraries.