Patrickkx Patrickkx - 1 month ago 6
Javascript Question

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...

Answer

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