javascript带参数的构造函数定义一个对象Circle(圆)

来源:百度知道 编辑:UC知道 时间:2024/06/06 07:52:38
用带参数的构造函数定义一个对象Circle(圆),它的三个属性分别是x、y和radius(代表圆心的x、y坐标和圆的半径),在Circle函数的prototype上添加三个方法getDiameter、getCircumference和getArea,它们分别返回圆的直径、周长和面积。编写代码测试这个对象。

<script type="text/javascript">
function Circle(x,y,radius){
this.x=x;
this.y=y;
this.radius=radius;
}
Circle.prototype.getDiameter=function(){
return 2*this.radius;
}
Circle.prototype.getCircumference=function(){
return Math.PI*2*this.radius;
}
Circle.prototype.getArea=function(){
return Math.PI*this.radius*this.radius;
}
//test for the object;
var circle1=new Circle(10,10,10);
alert(circle1.getDiameter());
alert(circle1.getCircumference());
alert(circle1.getArea());
</script>