//定义Circle类,拥有成员变量r,常量PI和计算面积的成员函数area()
function Circle(radius)
{
this.r = radius;
}
Circle.PI = 3.14159;Circle.prototype.area = function( ) {return Circle.PI * this.r * this.r;}
//使用Circle类var
c = new Circle(1.0);
alert(c.area());
function compute_area(){return Circle.PI * this.r * this.r;}Circle.prototype.area=compute_area;
//定义ChildCircle子类
function ChildCircle(radius)
{
this.base=Circle;
this.base(radius);
}
ChildCircle.prototype=new Circle(0);
function Circle_max(a,b)
{
if (a.r > b.r) return a;
else return b;
}
ChildCircle.max = Circle_max;
//使用ChildCircle子类
var c = new ChildCircle(1);
var d = new ChildCircle(2);
var bigger = d.max(c,d);
alert(bigger.area());
var newCircle=
{
r:1.0,
PI:3.1415,
area: function(){ return this.PI * this.r * this.r;}
};
alert(newCircle.area());
新闻热点
疑难解答