If I have a class:
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
getArea() {
return Rectangle.area(this.height, this.width);
}
static area(height, width) {
return height * width;
}
}
Then I can make an instance of that class and call my getArea
function on the instance:
var x = new Rectangle(5,6);
console.log(x.getArea());
Now, say for some reason in my getArea
function, instead of calling Rectangle.area
directly, I wanted to find the class dynamically and call the static method on whatever dynamically the class instance is:
getArea() {
return this.class.area(this.height, this.width);
}
In PHP, I can do that by something like static::area()
or self::area()
. How can I do something similar to static
and self
in Javascript to dynamically access static methods from the class that this
belongs to?