简体   繁体   English

无法在自己的类中调用静态函数

[英]Unable to call static function inside its own class

How can I call static function inside the class itself? 我如何在类本身内部调用静态函数? I try self keyword instead of this but I still get error. 我尝试使用self关键字代替它,但是仍然出现错误。

 class Test { static staticFunction() { console.log('Inside static function.'); } regularFunction() { this.staticFunction(); } } let test = new Test(); test.regularFunction(); 

You can reference the static function through the class name, like this: 您可以通过类名称引用静态函数,如下所示:

 class Test { static staticFunction(input) { console.log('Inside static function.'); } regularFunction() { Test.staticFunction(); } } let test = new Test(); test.regularFunction(); 

You can't use the this reference to access a static function. 您不能使用此引用来访问静态函数。 You should just do staticFunction(input) or even better Test.staticFunction(input). 您应该只执行staticFunction(input)或什至更好的Test.staticFunction(input)。

不能在静态方法或类中使用“ this”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM