26_Getter

本题考点:class类、getter

根据题目要求,当通过“Rectangle”类创建一个实例的时候,调用该实例的“area”属性返回矩形的面积,核心步骤有:

  1. 构造函数中需要两个参数分别为“width”和“height”
  2. 可以选择创建一个返回“width”与“height”乘积的方法
  3. 对“area”函数使用“get”计算,该函数返回第二步的方法

参考答案

class Rectangle {
    constructor(height, width) {
        this.height = height
        this.width = width
    }
    get area() {
        return this.calcArea()
    }
    calcArea() {
        return this.height * this.width
    }
}