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!