Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a method from another method in the same class

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();
like image 520
steve-o Avatar asked Apr 26 '17 19:04

steve-o


People also ask

Can you call a method within a method?

Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile. And in java 8 and newer version you achieve it by lambda expression.

How do you call another method in the same class python?

To call a function within class with Python, we call the function with self before it. We call the distToPoint instance method within the Coordinates class by calling self.

How do you call a method inside a class in Java?

To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main. java).

How do you call a method from a method in Java?

A method must be created in the class with the name of the method, followed by parentheses (). The method definition consists of a method header and method body. We can call a method by using the following: method_name(); //non static method calling.


4 Answers

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();
like image 137
Toby Mellor Avatar answered Oct 19 '22 12:10

Toby Mellor


You need to use this not self like

runMyTest() {
  this.myTest();
}

However a lot of implementations like to keep the reference and are doing the following:

var self = this;

That might be the reason you were thinking of self as self reference. For further reading I'd suggest this SO - post

like image 31
edi Avatar answered Oct 19 '22 12:10

edi


class MyClass {

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

    runMyTest() {
      this.myTest();
    }

}

var myClass = new MyClass();
myClass.runMyTest();
like image 25
M Melnikov Avatar answered Oct 19 '22 11:10

M Melnikov


class MyClass {

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

runMyTest = ()=>{
 this.myTest();  
}

runMyTest2 = function(){
 this.myTest(); 
}

}

var myClass = new MyClass();

myClass.runMyTest(); myClass.runMyTest2();

use arrow function to bind this to the global object. To represnt objects properties we have to use function

 let user = {
    name :"Something
 }

 user.value = ()=>{
   console.log(this)
 }

 user.value2 = function(){
   console.log(this)
 }

   user.value(); ///returns this for windows
   user.value2(); ///returns object user  
like image 3
Rahul Somaraj Avatar answered Oct 19 '22 10:10

Rahul Somaraj