MarcusDidius
New member
Hallo,
ich versuche mich mit Vererbung in JS zu beschäftigen.
Die Zeile console.log(t.toString()); gibt nur einen Leerstring aus. Ich hätte erwartet, dass sie 'Triangle' ausgibt. Was mache ich falsch?
Vielen Dank,
Hans
ich versuche mich mit Vererbung in JS zu beschäftigen.
Code:
function Shape() {
this.name = 'Shape';
this.toString = function() {
return name;
};
}
function TwoDShape() {
this.name = '2DShape';
}
function Triangle(side, height) {
this.name = 'Triangle';
this.side = side;
this.height = height;
this.getArea = function() {
return side * height * 1/2;
};
}
TwoDShape.prototype = new Shape();
Triangle.prototype = new TwoDShape();
TwoDShape.prototype.constructor = TwoDShape;
Triangle.prototype.constructor = Triangle;
var t = new Triangle(2, 3);
console.log(TwoDShape.prototype);
console.log(t.toString());
Die Zeile console.log(t.toString()); gibt nur einen Leerstring aus. Ich hätte erwartet, dass sie 'Triangle' ausgibt. Was mache ich falsch?
Vielen Dank,
Hans