//定義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());
新聞熱點
疑難解答