简体   繁体   中英

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

I'm having a little problem with my class. It got to be something very easy, but I just cant find out the solution. Class Cuboid works well, but the class Cube is just not okey, I think I've used the super method in a wrong way.

Just give me a little hint. Thank you in advance.

 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; } } 

Guys, why do you downvote my question? It's not really nice...

As I suggesting Cube is Cuboid with all 3 dimension equal. So there is 2 options to do it:

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;
  }
  // ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM