新的起点 新的开始

快乐生活 !

js实相继承小例

本文通过混合构造函数于原型方式来实现JS的继承功能。 Polygon为父类,Triangle Rectangle 为子类。
function Polygon (iSiders){
    this.sides = iSiders;
}
Polygon.prototype.getArea = function(){
    return 0;
}


function Triangle(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;
}

function Rectangle(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;
}
var triangle = new Triangle(12,4);
var rectangle = new Rectangle(22,10);
alert(triangle.getArea);
alert(rectangle.getArea);

posted on 2007-04-10 14:23 advincenting 阅读(236) 评论(0)  编辑  收藏 所属分类: 脚本


只有注册用户登录后才能发表评论。


网站导航:
 

公告

Locations of visitors to this page

导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(13)

随笔分类(71)

随笔档案(179)

文章档案(13)

新闻分类

IT人的英语学习网站

JAVA站点

优秀个人博客链接

官网学习站点

生活工作站点

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜