In @Ninjaneer 's accepted answer:
class StaticMethodCall { constructor() {
console.log(StaticMethodCall.staticMethod());
// 'static method has been called.'
console.log(this.constructor.staticMethod());
// 'static method has been called.' }
static staticMethod() {
return 'static method has been called.'; } }
this.constructor.staticMethod()
will throw an error because you must call the super constructor before accessing this
in the constructor.
Assuming you fix this aforementioned issue, no pun intended, I think the only benefit of calling this.constructor.staticMethod()
is that you're not dependent on the name of the class -- if it ever changes. However, this benefit is probably insignificant since it only benefits static method calls made from inside the class. Static method calls made externally would have to be something like StaticMethodCall.constructor.staticMethod()
which is defeats the purpose and looks silly.
In Summary:
If you plan on using the static method outside of the class, I'd definitely stick with using StaticMethodCall.staticMethod() since it's more idiomatic and concise.
If you only plan on using the static method within the class, then maybe it's ok to to use this.constructor.staticMethod()
, but it'll probably confuse other developers and lead them back to this page :-)
0
Created by Chris C. on 2020-03-12 02:48:41 +0000 UTC
Share