【发布时间】:2015-05-28 10:36:55
【问题描述】:
我尝试使用实例方法中的静态成员。我知道accessing static member from non-static function in typescript,但我不想硬编码类以允许继承:
class Logger {
protected static PREFIX = '[info]';
public log(msg: string) {
console.log(Logger.PREFIX + ' ' + msg); // What to use instead of Logger` to get the expected result?
}
}
class Warner extends Logger {
protected static PREFIX = '[warn]';
}
(new Logger).log('=> should be prefixed [info]');
(new Warner).log('=> should be prefixed [warn]');
我尝试过类似的东西
typeof this.PREFIX
【问题讨论】:
-
TLDR -
ClassName.property。对于被覆盖的属性,(<typeof ClassName> this.constructor).property.
标签: typescript