Calling a method from another method in the same class

后端 未结 7 1426
难免孤独
难免孤独 2021-02-05 01:21

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?

7条回答
  •  温柔的废话
    2021-02-05 01:38

    class MyClass {
    
      myTest() {
       console.log('it works');
      }
    
      let runMyTest = ()=>{
       this.myTest();
      }
    
    }
    
    var myClass = new MyClass();
    
    myClass.runMyTest();
    

    use arrow function to bind this within a function. it works fine even on typescript

提交回复
热议问题