給定以下類的層次結構:ChildClass
extends ParentClass
,是否可以從ParentClass
構造函數中訪問ChildClass
構造函數?例如:從基類構造函數訪問子構造函數
class ChildClass extends ParentClass
{
constructor()
{
super()
}
}
ChildClass.prop = 'prop'
class ParentClass
{
constructor()
{
if (this._child().prop == 'prop') // here
{
console.log("All ok");
}
}
get _child()
{
return this.constructor;
}
}
換句話說,我想要做的是進入孩子的「靜態」屬性進行覈查的目的
是的,這是可能的。不,「this.constructor」不是「髒代碼」,而是一種常用的方法來達到靜態屬性。如果這是一個設計錯誤,這取決於情況。 – estus