为什么说音量没有定义?

时间:2019-05-24 17:32:19

标签: javascript function class object

在卷上发现非法退货声明错误...

试图添加CuboidMaker15.volume来调用对象

class CuboidMaker15 {
    constructor(cuboidMaker15Attributes){
      this.length = cuboidMaker15Attributes.length;
      this.width = cuboidMaker15Attributes.width;
      this.height = cuboidMaker15Attributes.height;
    }
}
   volume(); {
    return this.length * this.width * this.height;
  }

surfaceArea(); {
  return 2 * (this.length * this.width + this.length * this.height + this.width * this.height);
}

const cuboid15 = new CuboidMaker15({
  length: 4,
  width: 5,
  height: 5
});

一直说未定义音量...

1 个答案:

答案 0 :(得分:2)

您需要在类定义内移动方法。还要在方法名称后删除;

class CuboidMaker15 {
  constructor(cuboidMaker15Attributes) {
    this.length = cuboidMaker15Attributes.length;
    this.width = cuboidMaker15Attributes.width;
    this.height = cuboidMaker15Attributes.height;
  }
  volume() {
    return this.length * this.width * this.height;
  }

  surfaceArea() {
    return 2 * (this.length * this.width + this.length * this.height + this.width * this.height);
  }
}

const cuboid15 = new CuboidMaker15({
  length: 4,
  width: 5,
  height: 5
});


console.log(cuboid15);
console.log(cuboid15.volume());
console.log(cuboid15.surfaceArea());