https://jsfiddle.net/bap2u530/1/
Hello Guys,
Link to my jsfiddle is above. You can view Js code below attached.
I got the whole thing working however, I’m not sure I understand how this all works…
When I passed sideLenght as a paremeter of super and commented out this.sideLength in Square class, console.log(square) gives { name: ‘square’, sides: 4, sideLength: undefined }.
I thought that with usage of super I tell that I get access to parent’s properties and methods. It looked naturally, that if I pass the sideLength parameter (like super(sideLength) and comment out this.sideLength = sideLength) it will be taken to the Square class that extends. But it keeps giving me undefined and the result as NaN
Every other solution like:
super(sideLength)
this.sideLength = sideLength;
works, however it feels like I’m doubling the work. Could someone explain which is the best practice here? Or I got something wrong?
class Shape{
constructor(name,sides,sideLength){
this.name = name;
this.sides = sides;
this.sideLength = sideLength;
}
calcPerimeter(){
return this.sides * this.sideLength
}
}
class Square extends Shape {
constructor(sideLength){
super(); //super(sideLength) console.log(square) gives { name: ‘square’, sides: 4, sideLength: undefined }
this.name = ‘square’;
this.sides = 4;
this.sideLength = sideLength;
}
calcArea(){
return this.sideLength * this.sideLength
}
}
let square = new Square(5)
console.log(square.calcArea(), square.calcPerimeter())