4

Its my understanding that this doesn't work (this is a contrived example - see the RxJS for what I am actually running):

function Foo() {
  this.name = 'Johnny Cash'
}

Foo.prototype.who = () => {
  console.log(this.name) // undefined
};

var foo = new Foo();

foo.who()

As this doesnt have the correct scope. However this page (last 2 bottom examples) on RxJS docs use it. How are they running this code?

Is the code on the RxJS page incorrect? or do I need to run through some kind of Babel plugin ( I already tried running through babel-require and babel-polyfill with same effect)

cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • 1
    `Foo` and `Emitter` are entirely unrelated in this example. It is possible in any number of ways to make `foo` correspond to `this`, the most straight forward of which is `Emitter.prototype.who.call(foo)`. Beyond this, it's rather unclear what you're asking. – deceze Jun 01 '16 at 01:23
  • 1
    There's also not a single "arrow function" here. – deceze Jun 01 '16 at 01:24
  • Lo.l. My bad. I was trying to contrive an example. Typos fixed – cyberwombat Jun 01 '16 at 01:27
  • 1
    That sample code does look wrong; maybe it was just a mistake by the author. – Jacob Jun 01 '16 at 01:30
  • @zerkms well if I run this `this.name` is undefined. So I am assuming this is not in the scope I expect - perhaps thats a better phrasing. Other SO questions similar seem to concur that this is not possible. So why or how is RxJS doing this? I am trying to run their code but cannot even with Babel – cyberwombat Jun 01 '16 at 01:30
  • Don't use arrow functions to assign prototype methods as an arrow function's *this* is lexical. In the OP, *this* within *who* will always be the global object. Use a function expression instead so that *this* is set by the call. In a browser, *this.name* will reference *window.name*, so return the window's name (if one has been assigned). – RobG Jun 01 '16 at 01:31
  • @SebastianG.Marinescu - yes. How is the example from RxJS supossed to work? I just get undefined errors unless I get rid of the arrow functions and go back to 'standard' function definitions – cyberwombat Jun 01 '16 at 01:31
  • @RobG so you are agreeing that the RxJS docs are incorrect yes? – cyberwombat Jun 01 '16 at 01:35
  • I'm kinda inclined to close this as a duplicate of [ES6 arrow functions not working on the prototype?](http://stackoverflow.com/q/31755186/1048572). If your question is really only about the RxJS docs, I think that's off-topic here and should be a simple bug report – Bergi Jun 01 '16 at 01:54
  • @Bergi you are probably right. When I saw the code in the docs I questioned my own knowledge so came to SO but yes. A big report is being filed now. – cyberwombat Jun 01 '16 at 02:39

1 Answers1

2

Examples on that page are broken.

It would be fair to assume it was never run as it is currently posted, since it has the syntax error in the

var subcription = emitter.listen('data', data => console.log(`data: ${data}`);

line (not paired parentheses).

After it is fixed and run - there are other exceptions about reading properties of undefined, which is this inside the beforementioned arrow functions.

zerkms
  • 249,484
  • 69
  • 436
  • 539