簡體   English   中英

具有特定 THIS 類型的環境 TypeScript 函數

[英]Ambient TypeScript functions with specific THIS type

我們怎樣才能在聲明環境打字稿一個功能,就是所謂的與this設置為特定類型的?

您可以使用this: Type作為函數的第一個參數:

class MyClass {
    constructor(public myText: string) {
    }
}

// enforce the context to be of type MyClass for this function
function myFunction(this: MyClass) {
    alert(this.myText);
}

var myObject = new MyClass("Hello");
myFunction.call(myObject);              // good
myFunction();                           // bad

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM