I have F which inherits from Shape.
F.prototype = Shape.prototype;
F creates new method with name test.
F.prototype.test = function(){return 'test';};
I understand that if I write F.prototype = Shape.prototype;
all the methods which I create in F will be available by other class which inheritance from Shape.
What have I done wrong?
Why do I get errors when I execute the code 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());
in this example when F
which inherits from Shape
and Triangle
created from F
jsFiddle demo woek well