How can I refer to a class from a static method without using the class name itself in JavaScript (similar to PHP\'s self
and self::method_name
)?<
You can use the this keyword to refer to the object itself.
See example below:
class FooBar {
static foo() {
return 'foo';
}
static bar() {
return 'bar';
}
static foobar() {
return this.foo() + this.bar();
// self::foo() + self::bar() would have been more desirable.
}
}
const res = FooBar.foobar();
console.log(res);