javascript - JS : EcmaScript6 how to pass different number of parameters to extended class

标签 javascript ecmascript-6

我的类(class)遇到了一些问题。这一定是一件非常简单的事情,但我就是找不到解决方案。类 Cuboid 工作得很好,但类 Cube 不太好,我想我以错误的方式使用了 super 方法。

给我一​​点提示。预先感谢您。

class Cuboid {
  constructor(length, width, height) {
    this.length = length;
    this.width = width;
    this.height = height;
  }
  get surfaceArea() {
    return (this.length * this.width + this.length * this.height + this.height * this.width) * 2;
  }
  get volume() {
    return this.length * this.width * this.height;
  }
}
class Cube extends Cuboid {
  constructor(length) {
    super(length);
    this.height = length;
  }
}

伙计们,你们为什么否决我的问题?这不太好...

最佳答案

正如我所建议的,立方体是所有 3 维相等的长方体。因此有两种选择:

1.

class Cube extends Cuboid {
  constructor(length) {
    super(length, length, length);
  }
}

2.

class Cuboid {
  constructor(length, width, height) {
    this.length = length || 0;
    this.width = width || this.length;
    this.height = height || this.width;
  }
  // ....

关于javascript - JS : EcmaScript6 how to pass different number of parameters to extended class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40538199/

相关文章:

javascript - 下拉菜单: open links in new tab + "Go" link

javascript - 如何处理对google API google.maps.DistanceMatrixService.getDistanceMatrix()的多个请求?

javascript - 计算日期差异,获取小时、分钟和秒的差异 - 最好通过 js,否则 moment.js

javascript - Robin Herbots jquery.inputmask 允许多个值以逗号分隔

compilation - typescript :使用 lib.core.es6.d.ts

javascript - ES6 Module Loader中简单import语句和System.import的区别

javascript - 如何让图像在鼠标悬停时出现并在鼠标移出时隐藏

javascript - 在 ES6 中以数字作为索引创建 HashMap 的最佳方法

javascript - 根据匹配后的计算设置对象数组内对象属性的状态 : reactjs

JavaScript - ES6 函数由于某些奇怪的原因无法工作