简体   繁体   English

此TypeScript语法是什么意思?

[英]What does this TypeScript syntax mean?

I am watching a video about RxJS and have come across some TypeScript syntax I've not seen before. 我正在观看有关RxJS的视频,并且遇到了一些以前从未见过的TypeScript语法。 It doesn't matter that it's RxJS, it's a generic question, but I thought I'd just add that it was RxJS. 没关系,它是RxJS,这是一个通用的问题,但是我想我只是补充说它是RxJS。

It's the call to super() in this that I don't understand: 我不明白这是对super()的调用:

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? 我对super()的调用很感兴趣:这是怎么回事? 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? 谁能阐明?

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. Rx.Observable构造函数带有一个函数参数,该参数本身带有一个订户-一个具有“下一个”(由代码确认)和“错误” /“完整”方法的对象,尽管这些未在代码中使用以上。 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. 从视频所描述的内容来看,这对于扩展Rx功能也是有意义的-在将其设置为this.observer并让Rx.Observable继续执行其操作之前,您实际上是在“捕获”观察者并将其传递一个附加的初始值。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM