二、JavaScript 对象继承
程序员文章站
2022-03-08 08:29:43
...
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; } }
上一篇: MySQL 二进制如何安装