admin管理员组文章数量:1429865
Accessing the super
value of a getter
in a derived class doesn't seem to work:
class Foo {
private _message:string = "Hello,";
public get Message():string {
return this._message;
}
}
class Bar extends Foo {
public get Message():string {
return super.Message + " World";
}
}
var snafu:Bar = new Bar();
document.write(snafu.Message);
// Expected: "Hello, World"
// Actual: "undefined World"
How can I correctly override a getter
and make use of the super
value?
Accessing the super
value of a getter
in a derived class doesn't seem to work:
class Foo {
private _message:string = "Hello,";
public get Message():string {
return this._message;
}
}
class Bar extends Foo {
public get Message():string {
return super.Message + " World";
}
}
var snafu:Bar = new Bar();
document.write(snafu.Message);
// Expected: "Hello, World"
// Actual: "undefined World"
How can I correctly override a getter
and make use of the super
value?
- 3 This is just one of many "gotchas" in TypeScript inheritance. TS looks so much like C# that it fools you into thinking that it acts like C# too. See blog.wouldbetheologian./2012/11/… for several more :-(. – Ken Smith Commented Jan 22, 2013 at 3:03
1 Answer
Reset to default 8I'm not necessarily endorsing that you continue with this approach, but...
class Bar extends Foo {
public get Message():string {
return Object.getOwnPropertyDescriptor(Foo.prototype, 'Message').get.apply(this) + ' World';
}
}
Prototypal inheritance doesn't make this particularly straightforward.
本文标签: javascriptHow can I access the superclass value of a getter in a subclassStack Overflow
版权声明:本文标题:javascript - How can I access the superclass value of a getter in a subclass? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745444115a2658581.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论