0

I am watching a video about RxJS and have come across some TypeScript syntax I've not seen before. It doesn't matter that it's RxJS, it's a generic question, but I thought I'd just add that it was RxJS.

It's the call to super() in this that I don't understand:

class MyObservable extends Rx.Observable {
    constructor(initialValue) {
        super(observer) => {
            observer.next(initialValue);
            this.observer = observer;
        }
    }
}

The video goes on to point out that this is not a good way to extend observable functionality, but that's not what I'm interested in anyway. I'm interested in the call to super(): what's going on here? The best that I can come up with is that it is shadowing the base class's constructor, but I'm not sure. Can anyone elucidate?

serlingpa
  • 12,024
  • 24
  • 80
  • 130
  • 1
    Possible duplicate of [ES6 What does super() actually do in constructor function?](https://stackoverflow.com/questions/42582147/es6-what-does-super-actually-do-in-constructor-function) – Estus Flask May 04 '18 at 16:43
  • 2
    I'm guessing it's a typo, and that it's supposed to be passing an anonymous function to `super`, and the parenthesis is misplaced. Likely it should be `super(observer => { ... })`. – CRice May 04 '18 at 16:43

1 Answers1

2

This is definitely a typo. The constructor for Rx.Observable takes a function argument, which itself takes a Subscriber - an object with 'next' (as confirmed by your code), and 'error'/'complete' methods, although those aren't used in the code above. This was almost certainly meant to be

class MyObservable extends Rx.Observable {
    constructor(initialValue) {
        super((observer) => {
            observer.next(initialValue);
            this.observer = observer;
        });
    }
}

This also makes sense from what the video is describing, about extending Rx functionality - you're effectively 'capturing' the observer and passing it an additional initial value, before setting it to this.observer and letting Rx.Observable continue to do its thing.

Luiz Scheidegger
  • 788
  • 5
  • 12
  • Yes, I just checked with WebStorm and this is indeed a syntax error. Surprised it made it into the video actually. – serlingpa May 04 '18 at 23:31