首页 > 在《javascript面向对象》书中uber-子对象访问父对象的方式的代码报错

在《javascript面向对象》书中uber-子对象访问父对象的方式的代码报错

<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>
<div style="width:100px;height:100px;background:red;" onclick="myclick()"></div>
</body>
</html>

【热门文章】
【热门文章】