101

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.

Shlomi
  • 3,622
  • 5
  • 23
  • 34
  • I've posted a more complex answer in a separate thread regarding this issue: http://stackoverflow.com/a/43694337/3182819 – Thomas Urban May 01 '17 at 08:04

3 Answers3

80

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

Saksham
  • 9,037
  • 7
  • 45
  • 73
Otto Nascarella
  • 2,445
  • 2
  • 17
  • 8
  • 14
    OP is not asking about calling it from an instance. (If he was, it would be a duplicate of the question you linked) – Bergi Dec 18 '17 at 19:52
  • 2
    That's not even an answer. `this.constructor.is('prod')` would throw error in this case. – UtkarshPramodGupta Sep 06 '19 at 13:13
  • This is the solution when you want to call your static method when you are inside an instantiated class, Otherwise you just call `this.method()`. Also when you extend a class you call `super.method()` –  Jan 18 '20 at 15:02
  • 1
    Downvote because this doesn't answer the question. – Clonkex Nov 01 '21 at 03:29
64

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.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 23
    i think It's quite confusing use "this" keyword in a static context, but anyway it's working fine, thanks – Shlomi Jun 29 '15 at 14:07
  • @ShlomiSasson: Why? `this` is the class object on which the method was called, pretty standard… Is this greatly different in other languages? – Bergi Jun 29 '15 at 14:08
  • 6
    Yes, in PHP $this refer to the current object you're within it's method, into class' methods (static) you don't have this keyword exist (you have 'self' and newest 'static'). As far as i remember, also in Java "this" keyword isn't available into a static method - and it's make sense: there is no "this" entity when you're into a static context.. "this" must talking about particular entity and into static method you're referring for the whole class or for general actions.. – Shlomi Jun 29 '15 at 14:10
  • 12
    As a JS developer, it's very important to understand how `this` behaves. It does not work like `this` in other languages. – loganfsmyth Jun 29 '15 at 14:40
  • How to invoke the static method inside the **class constructor**? – kidwon Jan 10 '16 at 17:48
  • 1
    @kidwon: A constructor invocation is quite like a method invocation, so it's [the same as there](http://stackoverflow.com/q/28627908/1048572) (with the only difference of `this` not being usable before the super call). – Bergi Jan 10 '16 at 22:12
  • 1
    @kidwon `constructor(){ CLASS_NAME.STATIC_METHOD(); }` – Morteza Tourani May 13 '16 at 23:38
  • @kidwon Alternatively to using `this.constructor.method`, in the constructor you can also use `new.target.method`. – Bergi Dec 18 '17 at 19:56
47

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.

Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137