【问题标题】:this keyword for function parameter函数参数的 this 关键字
【发布时间】:2017-10-25 03:09:31
【问题描述】:

最近当我使用 Rxjs 5 时,我使用 npm install Rxjs@5.0.1 下载了 Rxjs,从 node_modules 下的下载代码中,我在 Rxjs 文件夹中找到了 Observable.d.ts,我看到它声明了它的构造函数,如下所示:

 *
 * @constructor
 * @param {Function} subscribe the function that is  called when the Observable is
 * initially subscribed to. This function is given a Subscriber, to which new values
 * can be `next`ed, or an `error` method can be called to raise an error, or
 * `complete` can be called to notify of a successful completion.
 */
constructor(subscribe?: <R>(this: Observable<T>, subscriber: Subscriber<R>) => TeardownLogic);

我的问题是:这个关键字在 subscribe 的函数类型声明中的用法是什么?:(this: Observable, ...),TypeScript 是否有一些关于这个关键字用法的文档,例如这里?谢谢。

【问题讨论】:

    标签: typescript rxjs5


    【解决方案1】:

    您可以(从 typescript 2.0 版开始)指定调用函数时所期望的 this 是什么。

    Specifying the type of this for functions中所述:

    在一个类或一个类中指定 this 的类型 接口、函数和方法现在可以声明它们的类型 期待。

    默认情况下,函数中 this 的类型是 any。从...开始 TypeScript 2.0,你可以提供一个显式的 this 参数。这 参数是参数列表中最先出现的假参数 函数的

    请注意,这不会被翻译成 js,所以它不是函数中的真正参数。

    【讨论】:

    • 谢谢,帮我找资料。
    猜你喜欢
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 2011-06-27
    • 2013-12-17
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多