简体   繁体   English

TypeScript 中的 (this:ClassName) 参数是什么意思?

[英]What does (this:ClassName) parameter mean in TypeScript?

I am a newbie to TypeScript.我是 TypeScript 的新手。 I was confused what (this:ClassName) parameter of a function mean in an abstract class.我很困惑 function 的 (this:ClassName) 参数在抽象 class 中的含义。 For example:例如:

abstract class Department{
   abstract describe(this:Department):void;
}

class ITDepartment extends Department{
    describe(){
        console.log('IT Department');
    }
}

As above, the implementing child class does not have any parameter for describe() method, but the parent class has the parameter of (this:Department).如上,实现子 class 没有 describe() 方法的任何参数,但父 class 有 (this:Department) 的参数。 What is this code telling me and why does this code work??这段代码告诉我什么以及为什么这段代码有效?

welcome to SO.欢迎来到SO。

In TypeScript, if you've after a variable or parameter declaration a colon its defining the type of the given variable.在 TypeScript 中,如果您在变量或参数声明之后有一个冒号,则它定义了给定变量的类型。
In your case you're declaring the this variable as type Department .在您的情况下,您将this变量声明为类型Department
Normally you don't do this with class member functions, because the type of this is already known as the type of the class.通常你不会对 class 成员函数执行this操作,因为它的类型已经被称为 class 的类型。

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

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