1

How can I call static function inside the class itself? I try self keyword instead of this but I still get error.

class Test {
  static staticFunction() {
    console.log('Inside static function.');
  }
  regularFunction() {
    this.staticFunction();
  }
}

let test = new Test();
test.regularFunction();
quarky
  • 710
  • 2
  • 13
  • 36

3 Answers3

2

You can reference the static function through the class name, like this:

class Test {
  static staticFunction(input) {
    console.log('Inside static function.');
  }
  regularFunction() {
    Test.staticFunction();
  }
}

let test = new Test();
test.regularFunction();
Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
1

You can't use the this reference to access a static function. You should just do staticFunction(input) or even better Test.staticFunction(input).

IceRise
  • 19
  • 6
1

Can not use 'this' in static methods or classes

Ilker Eker
  • 101
  • 9