¿Por qué no puedo llamar a un método prototipado en Javascript?
Tengo F que hereda de la forma.
F.prototype = Shape.prototype;
F crea nuevo método con nombre test.
F.prototype.test = function(){return 'test';};
Entiendo que si escriboF.prototype = Shape.prototype;
todos los métodos que cree en F estarán disponibles por otra clase cuya herencia de Shape.
¿Qué he hecho mal?
¿Por qué recibo errores cuando ejecuto el código?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());
en este ejemplo cuandoF
que hereda deShape
yTriangle
creado a partir deF
jsFiddle demo woek bien