二、JavaScript 对象继承
程序员文章站
2022-03-08 10:00:38
...
JavaScript 对象继承
几何形状结构继承图
几何形状结构继承图
Polygon = function(iSides) { //多边形 this.sides = iSides; } Polygon.prototype = { getArea : function() { return 0; } } Triangle = function(iBase, iHeight) { //三角形 Polygon.call(this, 3); this.base = iBase; this.height = iHeight; } Triangle.prototype = new Polygon(); Triangle.prototype = { getArea : function() { return 0.5 * this.base * this.height; } } Rectangle = function(iLength, iWidth) { //矩形 Polygon.call(this, 4); this.length = iLength; this.width = iWidth; } Rectangle.prototype = new Polygon(); Rectangle.prototype = { getArea : function() { return this.length * this.width; } }
下一篇: hive第一课