用js代码实现找出下面题目中的对象,以及对象的属性和方法,并实现相应的功能定义一圆Cirlcle对象,包含半径r和圆心,圆心为“点”Point对象,point对象有x和y属性,构造一圆,该圆可以计算周长和面积; 计算该原的周长和面积?
时间: 2024-05-16 22:14:30 浏览: 10
以下是实现代码:
```javascript
//定义一个点对象
function Point(x, y) {
this.x = x;
this.y = y;
}
//定义一个圆对象
function Circle(r, point) {
this.r = r;
this.point = point;
}
//计算圆的周长方法
Circle.prototype.getCircumference = function() {
return 2 * Math.PI * this.r;
}
//计算圆的面积方法
Circle.prototype.getArea = function() {
return Math.PI * Math.pow(this.r, 2);
}
//创建一个圆对象
var circle = new Circle(5, new Point(0, 0));
//输出该圆的周长和面积
console.log("该圆的周长为:" + circle.getCircumference().toFixed(2));
console.log("该圆的面积为:" + circle.getArea().toFixed(2));
```
输出结果为:
```
该圆的周长为:31.42
该圆的面积为:78.54
```