1

I am trying to call a static method from a parent static method.

Here is what I'd like to achieve:

class Parent {
    static greet() {
        console.log(self.message()) // <-- what should that be?
    }
}

class Child1 extends Parent {
    static message() {
        return 'Hello, World'
    }
}

class Child2 extends Parent {
    static message() {
        return 'Hi, Everyone'
    }
}

My goal is to have Child1.greet() log Hello, World and Child2.greet() log Hi, Everyone.

What should I replace the self keyword with in the greet() method?

SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
  • Well, that was unexpected! `this` in a static context‽ If you make your passive-agressive comment into a serious answer, I'll accept it. – SteeveDroz Jan 27 '19 at 07:30
  • The calling context behavior is the same as everywhere else - `Child1.greet()` will call the `greet` function with a `this` of `Child1` (even if the `greet` function is found on the prototype rather than directly on the class object being referred to) – CertainPerformance Jan 27 '19 at 07:35
  • I understand, I naively thought that JS would behave like any other language for once. Oh, JavaScript, you'll never cease to amaze and horrify me at the same time! – SteeveDroz Jan 27 '19 at 07:36

0 Answers0