假設我有一些代碼:
class BuildingWithRecipe {
static recipeType = ""
findRecipe(){
doStuffWith([not this].recipeType);
}
}
class Furnace extends BuildingWithRecipe {
static recipeType = "smelting"
}
我希望能夠使用該類的實體方法內的關鍵字訪問類的靜態方法,因此當我擴展 BuildingWithRecipe(假設使用 Furnace)時,我可以簡單地設定靜態 recipeType 屬性,并且 findRecipe 方法將訪問當在 Furnace 實體上呼叫時 Furnace.recipeType 而不是 BuildingWithRecipe.recipeType。
這可能嗎?
uj5u.com熱心網友回復:
你可以這樣做this.constructor:
findRecipe(){
doStuffWith(this.constructor.recipeType);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362173.html
標籤:javascript 遗产
上一篇:從其他檔案訪問結構資料成員的值
