Shape を継承した F があります。
F.prototype = Shape.prototype;
F は test という名前の新しいメソッドを作成します。
F.prototype.test = function(){return 'test';};
F.prototype = Shape.prototype;
Fで作成したすべてのメソッドを記述すれば、Shapeから継承する他のクラスで利用できることを理解しています。
私は何を間違えましたか?
コードを実行するとエラーが発生するのはなぜalert(B.test());
ですか?
function Shape(){}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function() {return this.name;};
var F = function(){};
F.prototype = Shape.prototype;
F.prototype.test = function(){return 'test';};
function Triangle(side, height) {
this.side = side;
this.height = height;
}
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';
var my = new Triangle(5, 10);
alert(my.toString());
var Second_class = function(){};
Second_class.prototype = Shape.prototype;
B.prototype = new Second_class();
alert(B.test());
この例では、 jsFiddleF
から継承Shape
およびTriangle
作成された場合、デモはうまく機能しますF