Hello developers this my first time posting assesment. I came up with this solution for OOJS 2 skill test. I wanted to know is this okay?
Hi @fuhad479 and welcome to the community
Your “Shape” class is perfect!
For the “Square” class there are two improvements:
-
We can simplify the constructor. Since this class is specifically for squares we already know that the name will be “square” and that it will have four sides. Therefore only the side length needs to be provided when constructing a square:
constructor(sideLength) { super('square', 4, sideLength); }
-
You used the perimeter formula in
calcArea()
. This should be:this.sideLength * this.sideLength
If you have any questions just ask
Have a nice day,
Michael
Hey @mikoMK thanks for suggestion. So after the improvement when I’m calling super('square', 4, sideLength)
inside “Square” classes constructor
function is this working like this
constructor(sideLength) {
this.name = 'square'
this.sides = 4
this.sideLength = sideLength
}
I’m not sure I understand your question. So please tell me if the following statements don’t answer it
- The
constructor()
of “Shape” needs to be the same as before, but yes, it will get those three values like you wrote. - It wouldn’t be possible to just replace the “Square”
constructor()
with your new version since we need to usesuper()
in extended classes to initialize thethis
variable. - You could use
but this would make it unnecessarily complicated.constructor(sideLength) { super(); this.name = 'square' this.sides = 4 this.sideLength = sideLength }
I hope this help.
Michael
Okay I get it. Thanks for the help.
Hi Michael,
Thanks for your solution! I now have a better understanding of using the super function! I have a question tho.
I wrote the Square class like this:
class Square extends Shape {
constructor(sideLength) {
super(sideLength)
this.name = "Square"
this.sides = 4
this.sideLength = sideLength
}
}
Is there an issue with this aside from it complicating the code?
Hi @rdszz
I don’t think there’s an issue. You just don’t use the full potential of inheritance.
See you,
Michael