I am learning OOP with JavaScript based on MDN.
I created class Shape
and Square
.
class Shape {
name;
sides;
sideLength;
constructor(name, sides, sideLength) {
this.name = name;
this.sides = sides;
this.sideLength = sideLength;
}
calcPerimeter() {
const result = this.sides * this.sideLength;
console.log(`Perimeter is ${result}`)
}
}
class Square extends Shape {
constructor(sideLength) {
super(sideLength);
this.name = 'square';
this.sides = 4;
// this.sideLength = sideLength;
}
}
I expect class Square
to inherit from Shape
for property sideLength
. I try to create new object with new Square(5)
and it produces an object Square {name: 'square', sides: 4, sideLength: undefined}
. I create the class Square
by automatically assign value 4
to sides
and square
to name
.
Appreciate your advice why the value of sideLength
is undefined
.