<html>
<head>
<script>
function Shape() {}
// augment prototype
Shape.prototype.name = 'Shape';
Shape.prototype.toString = function () {
var constr = this.constructor;
return constr.uber
? this.constr.uber.toString() + ', ' + this.name
: this.name;
};
function TwoDShape() {}
// take care of inheritance
var F = function () {};
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.uber = Shape.prototype;
// augment prototype
TwoDShape.prototype.name = '2D shape';
function Triangle(side, height) {
this.side = side;
this.height = height;
}
// take care of inheritance
var F = function () {};
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.uber = TwoDShape.prototype;
// augment prototype
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function () {
return this.side * this.height / 2;
};
function myclick(){
var my = new Triangle(5,10);
console.log(my.toString());
}
</script>
</head>
<body>
<p style="width:100px;height:100px;background:red;" onclick="myclick()"></p>
</body>
</html>
兄弟你好,请问你当年把这个问题解决了吗……我现在也卡在这儿了T0T