Hi! I’d appreciate if someone could kindly check my work on Test your skills - Object-oriented JavaScript.
About task 2, I have a question. (Please kindly see below.)
My work Taks 1
Task 2
For the constructor of the subclass Square, I originally tried super(sideLength);
instead of this.sideLength = sideLength.
But that way, this.sideLength became “undefined” and couldn’t do the area calculation (it was returned as NaN ).
Even though the result printed on console is what I wanted, I still feel like my solution is not correct… Could you let me know how I should’ve done this?
Task 1 is fine.
Task 2: The constructor should only include a super( /* ... */ ) call. You will need to set all three parameters (either fixed values or variables). Do you know what I mean?
I’m sorry but I don’t really understand what that means…
From this example on the constructor page, I thought default values of parameters (name and sides) should be written inside the parentheses of constructor (), but that way, an error occurred.
Then I put the default values inside the parentheses of super(). That way the name “square” is correct in the console, but the area calculation is again NaN.
Since the examples in the lesson page is plainer than this test (no default values etc.), I’m completely lost.
Your constructor was fine with only the sideLength parameter.
Only the super() call needs all three, because of the three parameters in the Shape constructor. The correct code would be:
Because the square is a special version of the shape we can already prefill the first two parameters and only need to know the side length from the user. This makes the square constructor much easier.
Does that make sense?
Now I understand so much better about how to set some parameters to default values, and how to leave some parameters as variables, when inheriting those parameters from an existing class!
I fixed the constructor() and super(), and now it’s working.