ES6 - Call static method within a class

JavascriptOopEcmascript 6

Javascript Problem Overview


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?

TY.

Javascript Solutions


Solution 1 - Javascript

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();

https://stackoverflow.com/questions/28627908/es6-call-static-methods

Solution 2 - Javascript

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

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.

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.

Solution 3 - Javascript

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.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShlomiView Question on Stackoverflow
Solution 1 - JavascriptOtto NascarellaView Answer on Stackoverflow
Solution 2 - JavascriptBergiView Answer on Stackoverflow
Solution 3 - JavascriptChris SchmitzView Answer on Stackoverflow