<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> function Shape(){} Shape.prototype.name = 'shape'; Shape.prototype.toString = function () { var result = []; if(this.constructor.uber){ result[result.length] = this.constructor.uber.toString(); } result[result.length] = this.name; return result.join(','); } function TwoDShape(){} var F = function () {}; F.prototype = Shape.prototype; TwoDShape.prototype = new F(); TwoDShape.prototype.constructor = TwoDShape; TwoDShape.uber = Shape.prototype; TwoDShape.prototype.name = '2D shape'; function Triangle(side,height) { this.side = side; this.height = height; } var F = function () {}; F.prototype = TwoDShape.prototype; Triangle.prototype = new F(); Triangle.prototype.constructor = Triangle; Triangle.uber = TwoDShape.prototype; Triangle.prototype.name = 'Triangle'; Triangle.prototype.getArea = function () { return this.side*this.height/2; } </script> </body> </html>
3.1 uber 子对象访问父对象的方式
最新推荐文章于 2023-11-27 18:31:15 发布