Here is a JS class.
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.area); // 100
It seems to work fine. However, I don't understand why we need to specify the get
keyword for area()
but not for calcArea()
although both are returning values. Could someone explain this? And if possible, if someone could also explain this from react's perspective and whether this still applies for class components? Thanks.