Hello there ,
I have recently finished “Classes in Javascript” skill tests, and I want someone to review my solutions and point out any flaws in them. Here are the links,
OOPS 1 & Task link
OOPS 2 & Task link
Thank you in Advance,
Dinesh
Hello there ,
I have recently finished “Classes in Javascript” skill tests, and I want someone to review my solutions and point out any flaws in them. Here are the links,
OOPS 1 & Task link
OOPS 2 & Task link
Thank you in Advance,
Dinesh
Hi @Dinesh_Sake
Task 1 is perfect!
In task 2 we can use the inheritance to make the constructor simpler:
For the “Square” class we know the shape’s name and its number of sides. Therefore we only need the side length via the constructor. Then we use the two values and the variable to call super()
. We don’t need to save any variable inside the square class with this
. Everything goes directly to the super()
call.
I tried to keep the explanations a bit vague, because I know you like to figure the solution out yourself. Tell me if you need more help.
Cheers,
Michael
Hello sir,
I did do some changes as told by you, but I’m not sure if it is correct. Please see if it’s right.
This is already better, but we can still improve.
The goal is to just use const square = new Square(5);
. See, how we only have the side length? The constructor()
function only needs to take one parameter (sideLength). The other two values are always the same for a square (name: ‘square’ / sides: 4). So we can hardcode them into the super()
function: super('square', 4, sideLength);
Oh wow! It’s working. Thank you so much sir!!!
Yeah!!! That’s exactly what I meant.
I think you now understand the power of inheritance.