I’ve got my functions working for extending the Square clss from the Shape class. However, I don’t understand how super()
works in this assingment.
for example:
class Shape {
constructor(name, sides, sideLength) {
this.name = name;
this.sides = sides;
this.sideLength = sideLength;
}
calcPerimeter() {
console.log(this.sides * this.sideLength);
}
}
class Square extends Shape {
constructor(sideLength) {
super();
this.name = 'square';
this.sides = 4;
this.sideLength = sideLength;
}
calcArea() {
console.log(this.sideLength * this.sideLength);
}
}
let square = new Square(5);
square.calcPerimeter();
square.calcArea();
That’s the final code I got to complete the assignment, but if I comment out this.sideLength = sideLength
I get NaN
printed to the console. Also, why do I need to assign the sideLength
again in the constructor? Shouldn’t it suffice to use super(sideLength)
only?
I need help understanding the rules for using super()
, I thought that super binded this
for the passed in arguments in the constructor’s parameters. Why do I need to this.sideLength = sideLength
again and pass no arguments to super()
??