Calling a method from another method in the same class

You need to use the this keyword instead of self.

runMyTest() {
    this.myTest();
}

A side note

If you are nesting standard functions notation then this is not lexically bound (will be undefined). To get around this, use Arrow Functions (preferred), .bind, or locally define this outside of the function.

class Test {
  constructor() {
    this.number = 3;
  }

  test() {
    function getFirstThis() {
       return this;
    }

    const getSecondThis = () => {
       return this;
    };

    const getThirdThis = getFirstThis.bind(this);
    
    const $this = this;
    function getFourthThis() {
      return $this;
    }

    // undefined
    console.log(getFirstThis());
    
    // All return "this" context, containing the number property
    console.log(this); 
    console.log(getSecondThis());
    console.log(getThirdThis());
    console.log(getFourthThis());
  }
}

new Test().test();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)