admin管理员组文章数量:1323715
This my parent class, it has trigger
method which is public
method:
class BaseEffect {
//properties and contructor...
//other methods...
public trigger = (): void => void (this.target.hitPoint -= this.amount);
}
And a TurnBasedEffect
extends BaseEffect
class:
class TurnBasedEffect extends BaseEffect {
//properties and contructor...
//other methods...
public trigger = (): void => {
super.trigger();
this.remainingTurns--;
};
}
This class also has trigger
method, inside this method, a trigger
method of the parent class is called.
The problem is when a trigger
method of the derived class is called, typescript throws this error:
TypeError: (intermediate value).trigger is not a function
And point to this line of the trigger
method of the TurnBasedEffect
class:
//...
super.trigger();
//...
What's wrong with my classes and how to solve this problem?
This my parent class, it has trigger
method which is public
method:
class BaseEffect {
//properties and contructor...
//other methods...
public trigger = (): void => void (this.target.hitPoint -= this.amount);
}
And a TurnBasedEffect
extends BaseEffect
class:
class TurnBasedEffect extends BaseEffect {
//properties and contructor...
//other methods...
public trigger = (): void => {
super.trigger();
this.remainingTurns--;
};
}
This class also has trigger
method, inside this method, a trigger
method of the parent class is called.
The problem is when a trigger
method of the derived class is called, typescript throws this error:
TypeError: (intermediate value).trigger is not a function
And point to this line of the trigger
method of the TurnBasedEffect
class:
//...
super.trigger();
//...
What's wrong with my classes and how to solve this problem?
Share Improve this question asked Jun 15, 2020 at 1:51 Trí PhanTrí Phan 1,1933 gold badges16 silver badges35 bronze badges1 Answer
Reset to default 10You have a field that’s initialized to an arrow function. The initialization is done by the constructor:
class Foo {
bar = () => {};
}
console.log(new Foo().bar);
console.log(new Foo().hasOwnProperty('bar'));
console.log(Foo.prototype.bar);
The field doesn’t end up in the prototype chain, so it isn’t inherited, and you can’t call it through super
.
I suggest writing normal methods:
class BaseEffect {
//properties and contructor...
//other methods...
public trigger() {
this.target.hitPoint -= this.amount;
}
}
class TurnBasedEffect extends BaseEffect {
//properties and contructor...
//other methods...
public trigger() {
super.trigger();
this.remainingTurns--;
}
}
本文标签:
版权声明:本文标题:javascript - Typescript: (intermediate value).(...) is not a function when call a method of parent class from derived class - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124995a2421897.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论