[英]How to call a Parent class method from child class instance having super
我想做这样的事情但是出错了。 我在官方文档中找不到任何帮助,我认为这就像调用父方法一样简单。
“错误类型错误:childA.getChildOfType 不是函数”
export class Parent
{
constructor(public type: string)
{}
public getChildOfType = (type: string) =>
{
if(this[type] && this[type].length > 0) return this[type];
return null;
}
}
export class ChildA extends Parent
{
Plants = ['Amaryllis', 'African Violet', 'Bird Of Paradise'];
constructor(public type: string)
{
super(type);
}
}
export class ChildB extends Parent
{
Animals = ['Monkey', 'Tiger', 'Mouse'];
constructor(public type: string)
{
super(type);
}
}
const childA = new ChildA('Plants');
const childB = new ChildB('Animals');
console.log(childA.getChildOfType('Plants'));
console.log(childA.getChildOfType('Animals'));
尝试类似:
Parent.prototype.getChildOfType = (type: string) =>
{
if(this[type] && this[type].length > 0) return this[type];
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.