Javascript ES6 - 在类中调用静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31116300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ES6 - Call static method within a class
提问by Shlomi Sasson
I have this class which does an internal call to a static method:
我有这个类对静态方法进行内部调用:
export class GeneralHelper extends BaseHelper{
static is(env){
return config.get('env:name') === env;
}
static isProd(){
return GeneralHelper.is('prod');
}
}
Are there any keywords I can use to replace the class name in the line below:
是否有任何关键字可以用来替换以下行中的类名:
GeneralHelper.is('prod');
In PHP there are self
, static
etc. Does ES6 provide anything similar to these?
在PHP中也有self
,static
等不ES6提供类似于这些东西吗?
TY.
泰。
回答by Otto Nascarella
if you are calling the static function from inside an instance, the right way to refer to the class static function is:
如果您从实例内部调用静态函数,则引用类静态函数的正确方法是:
this.constructor.functionName();
this.constructor.functionName();
回答by Bergi
It's the same as calling a method on an ordinary object. If you call the GeneralHelper.isProd()
method, the GeneralHelper
will be available as this
in the method, so you can use
这与在普通对象上调用方法相同。如果您调用该GeneralHelper.isProd()
方法,GeneralHelper
将this
在该方法中可用,因此您可以使用
class GeneralHelper {
static is(env) { … }
static isProd(){
return this.is('prod');
}
}
This will however not work when the method is passed around as a callback function, just as usual. Also, it might be different from accessing GeneralHelper
explicitly when someone inherits isProd
from your class and overwrites is
, InheritedHelper.isProd()
will produce other results.
然而,当该方法作为回调函数传递时,这将不起作用,就像往常一样。此外,GeneralHelper
当有人isProd
从您的类继承并覆盖时,它可能与显式访问不同is
,InheritedHelper.isProd()
会产生其他结果。
If you're looking to call static methods from instance methods, see here. Also notice that a class which only defines static methods is an oddball, you may want to use a plain object instead.
如果您希望从实例方法调用静态方法,请参阅此处。另请注意,仅定义静态方法的类是一个奇怪的类,您可能希望改用普通对象。
回答by Chris Schmitz
Both of the answers here are correct and good, but I wanted to throw in an added detail based on this question title.
这里的两个答案都是正确和好的,但我想根据这个问题标题添加一个额外的细节。
When I saw "ES6 - Call static method within a class" it sounded like "call a static method (from a non-static method) within a class". Def not what the initial question asker is asking in the details.
当我看到“ES6 - 在类中调用静态方法”时,它听起来像是“在类中调用静态方法(从非静态方法)”。确定最初的问题提问者在细节中问的是什么。
But for anyone who wants to know how to call a static method from a non-static method within a class you can do it like this:
但是对于任何想知道如何从类中的非静态方法调用静态方法的人,您可以这样做:
class MyClass {
myNonStaticMethod () {
console.log("I'm not static.")
MyClass.myStaticMethod()
}
static myStaticMethod () {
console.log("hey, I'm static!")
}
}
MyClass.myStaticMethod() // will log "hey, I'm static!"
const me = new MyClass()
me.myNonStaticMethod() // will log "I'm not static" and then "hey, I'm static!"
The idea is that the static method is can be called without creating a new instance of the class. That means you can call it inside of a instance's method the same way you'd call it outside of the instance.
这个想法是可以在不创建类的新实例的情况下调用静态方法。这意味着您可以在实例的方法内部调用它,就像在实例外部调用它一样。
Again, I know that's not what the detail of the question was asking for, but this could be helpful other people.
同样,我知道这不是问题的详细信息所要求的,但这可能对其他人有所帮助。