Calling a method from another method in the same class

steve-o :

Why am I getting the error: "Uncaught TypeError: self.myTest is not a function"? How do I call a method from within another method in a javascript class?

class MyClass {

    myTest() {
      console.log('it works');
    }

    runMyTest() {
      self.myTest();
    }

}

var myClass = new MyClass();
myClass.runMyTest();

Toby Mellor :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javascript why calling a method from another method in the same class need this?

From Dev

Error with calling a method from another method in the same class

From Dev

Calling a method from another method inside the same class using getattr

From Java

Calling a method inside another method in same class

From Dev

Calling a list from one method to another in the same class in Python

From Java

Calling one method from another within same class in Python

From Java

Calling method of Another class from run() method

From Dev

Calling method of another parent of the same class

From Dev

Calling an object of a method of a class within another method of the same class

From Dev

Calling a class from another class with main method

From Dev

Calling a method from one class in another class

From Dev

Calling method from another method in the same class: which parameter is Python asking for?

From Dev

Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

From Dev

calling method from another method which takes 3 parameters in the same class

From Dev

Calling a constructor from method within the same class

From Java

Calling method of same name from different class

From Dev

calling a method from same class in xcode

From Dev

Calling a method from inside of another class

From Dev

Calling a method of the MainActivity from another class?

From Dev

Calling method from another class unsuccessful

From Java

java calling a method from another class

From Java

Calling service method from another service class

From Java

NullPointerException when calling method from another class

From Dev

JGroups RpcDispatcher calling method from another class

From Dev

calling render method from another class

From Dev

calling method in mainactivity from another class in android

From Dev

Python: Calling a decorator method from another class

From Dev

Having trouble calling a method from another class

From Dev

Syntax Error in Calling a method from another class

Related Related

  1. 1

    javascript why calling a method from another method in the same class need this?

  2. 2

    Error with calling a method from another method in the same class

  3. 3

    Calling a method from another method inside the same class using getattr

  4. 4

    Calling a method inside another method in same class

  5. 5

    Calling a list from one method to another in the same class in Python

  6. 6

    Calling one method from another within same class in Python

  7. 7

    Calling method of Another class from run() method

  8. 8

    Calling method of another parent of the same class

  9. 9

    Calling an object of a method of a class within another method of the same class

  10. 10

    Calling a class from another class with main method

  11. 11

    Calling a method from one class in another class

  12. 12

    Calling method from another method in the same class: which parameter is Python asking for?

  13. 13

    Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

  14. 14

    calling method from another method which takes 3 parameters in the same class

  15. 15

    Calling a constructor from method within the same class

  16. 16

    Calling method of same name from different class

  17. 17

    calling a method from same class in xcode

  18. 18

    Calling a method from inside of another class

  19. 19

    Calling a method of the MainActivity from another class?

  20. 20

    Calling method from another class unsuccessful

  21. 21

    java calling a method from another class

  22. 22

    Calling service method from another service class

  23. 23

    NullPointerException when calling method from another class

  24. 24

    JGroups RpcDispatcher calling method from another class

  25. 25

    calling render method from another class

  26. 26

    calling method in mainactivity from another class in android

  27. 27

    Python: Calling a decorator method from another class

  28. 28

    Having trouble calling a method from another class

  29. 29

    Syntax Error in Calling a method from another class

HotTag

Archive