I want to be able to access a method from both the instance and the class:
class AClass {
// I want to make this both static and normal
method() {
console.log("Method called");
}
}
I would expect to be able to access static
methods from the instance (like in python with @staticmethod
) but I can't as it gives an error:
class AClass {
static method() {
console.log("Method called");
}
}
AClass.method(); // this works
(new AClass).method(); // this doesn't work
Create a non-static method of the same name and call the static method from the non-static method.
class AClass {
static method() {
console.log("Method called");
}
method() {
AClass.method();
}
}
AClass.method(); // this works
(new AClass).method(); // this now works