简体   繁体   English

ES6 - 在类中调用静态方法

[英]ES6 - Call static method within a class

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 中有selfstatic等。 ES6 是否提供类似的东西?

TY.泰。

If you are calling the static function from inside an instance, the right way to refer to the static function of the class is:如果从实例内部调用静态函数,引用类的静态函数的正确方法是:

this.constructor.functionName();

Call static methods from regular ES6 class methods 从常规 ES6 类方法调用静态方法

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.此外,当有人从您的类继承isProd并覆盖is时,它可能与显式访问GeneralHelper不同, 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.另请注意,仅定义静态方法的类是一个奇怪的类,您可能希望改用普通对象。

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".当我看到“ES6 - 在类中调用静态方法”时,它听起来像是“在类中调用静态方法(从非静态方法)”。 Def not what the initial question asker is asking in the details.确定最初的问题提问者在细节中问的是什么。

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.同样,我知道这不是问题的详细信息所要求的,但这可能对其他人有所帮助。

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

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