Zobacz przykład dziedziczenia z placu zabaw w witrynie TypeScript:
class Animal {
public name;
constructor(name) {
this.name = name;
}
move(meters) {
alert(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
constructor(name) {
super(name);
}
move() {
alert("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
constructor(name) {
super(name);
}
move() {
alert(super.name + " is Galloping...");
super.move(45);
}
}
var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
Zmieniłem jedną linię kodu: alert w Horse.move(). Chcę uzyskać dostęp super.name, ale to po prostu wraca undefined. IntelliSense sugeruje, że mogę go używać, a TypeScript kompiluje się dobrze, ale nie działa.
Jakieś pomysły?