函数参数的 this 关键字 [英] this keyword for function parameter

查看:45
本文介绍了函数参数的 this 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Recently when I use Rxjs 5, I downloaded Rxjs by using npm install Rxjs@5.0.1, from downloaded code under node_modules, I found Observable.d.ts in Rxjs folder, I saw it declare its constructor like below:

 *
 * @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 关键字的用法是什么?: (this: Observable, ...), TypeScript 是否有一些关于此关键字用法的文档,例如这里?谢谢.

My question is: what is the usage of this keyword in function type declaration of subscribe?: (this: Observable, ...), Does TypeScript has some documentation for this keyword usage like here? Thank you.

推荐答案

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

You can (since version 2.0 of typescript) specify what is the this you're expecting when a function is invoked.

为函数指定 this 的类型:

在类或类中指定 this 的类型的后续操作接口、函数和方法现在可以声明它们的类型期待.

Following up on specifying the type of this in a class or an interface, functions and methods can now declare the type of this they expect.

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

By default the type of this inside a function is any. Starting with TypeScript 2.0, you can provide an explicit this parameter. this parameters are fake parameters that come first in the parameter list of a function

请注意,这不会被翻译成 js,因此它不是函数中的真正参数.

Notice that this won't get translated into js, so it's not a real argument in the function.

这篇关于函数参数的 this 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆