我想創建一個影像資料并重寫onload函式,但this在typescript中不能指向newImage。我怎么做才能使this指標指向newImage
uj5u.com熱心網友回復:
根據你提供的代碼,我沒有看到使用this指標作為newImg的方法。
但是你將能夠使用const{width,height} = newImg.somethingYouWannaDo()。
uj5u.com熱心網友回復:
你可以告訴TypeScript哪種型別的this,把它作為一個函式引數加入:
function onload(this: HTMLImageElement) {
const {width, height} = this
}
當編譯為JavaScript時,該引數將被忽略。
參見https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function
uj5u.com熱心網友回復:
function onload(img){
const {width, height} = img
}
onload沒有傳給img嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/326126.html
標籤:
上一篇:表單輸入上的HTML表格

