Assessment wanted for OOJS 3

Hi, been using this forum for help with some of the testing your skills portions of OOJS and had a question regarding ES6 inheritance syntax.

So I think I’ve got the solution here, but I was wondering why one only puts in sideLength in the Square constructor and everything else in super.

In a previous example on inheritance, when creating the new subclass of Teacher based on Person we pass in everything to Teacher and Person, do we not?
As shown here

class Teacher extends Person {
  constructor(first, last, age, gender, interests, subject, grade) {
    super(first, last, age, gender, interests);

    // subject and grade are specific to Teacher
    this.subject = subject;
    this.grade = grade;
  }
}

When I did that for OOJS 3 I got back error messages.

Why do we only need to pass one argument here as opposed to all of them in Teacher? Is there a reason for it described on that page I linked to that I’m glossing over?

I have gone through all the OOJS pages prior to this, but still having trouble understanding it if someone could help me with that.

Here is my solution to OOJS 3

class Shape {
    constructor(name, sides, sideLength) {
        this.name = name;
        this.sides = sides;
        this.sideLength = sideLength;
    }

    // Write your code below here

    calcPerimeter() {
        alert(`${this.sides * this.sideLength}`);
    }

}

class Square extends Shape {
    constructor(sideLength) {
        super('square', 4, sideLength);
    }

    calcArea() {
        alert(this.sideLength * this.sideLength);

    }
}

let square = new Square(10);
let triangle = new Shape('triangle', 3, 3);


// Calling the functions
square.calcArea();

Thanks!

Hi @patlo,
This is a great question. It made me think again of this section and thanks for posting it.

The way I understand it is this.

  1. When constructing a subclass the constructor statement is for the parameters for the new class we are creating.
    constructor(sideLength) {

In the case of the square subsclass it is based off the shape class. However the square class only needs one parameter sidelength as we already know that a square will have 4 sides and will always be named square.

  1. When creating a subclass based on the extends method we have to use the super to initialise the class we are basing the new subclass on otherwise we get the following error as stated by MDN
Uncaught ReferenceError: Must call super constructor in derived class before
accessing 'this' or returning from derived constructor

As MDN states in the same section

Unlike old-school constructor functions where the new operator does the initialization of this to a newly-allocated object, this isn’t automatically initialized for a class defined by the extends keyword, i.e the sub-classes.

In the case of the square we use the super statement to initialise the shape class but we only need an open parameter of sideLenth while the name and sides are given as constants i.e. hardcoded to the super.

  1. This means that when you need to create a square you only need to provide one arguement as the class only has one parameter with the other two parameters being hardcoded into the shape class.

I am a learner like you so please let me know if this makes any sense to you. It would really help me progress in the way I understand it as well.

Good luck with your studies.