Calling a method from another method in the same class

后端 未结 7 1424
难免孤独
难免孤独 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

    0 讨论(0)
  • 2021-02-05 01:40
    class MyClass(){
        constructor(){
             this.init();
        }
    
        init(){
             const $this = this;
             this.doSomethigElse();
        }
    
        doSomethingElse(){
             console.log("It worked!");
        }
    }
    
    
    0 讨论(0)
  • 2021-02-05 01:52

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

    0 讨论(0)
  • 2021-02-05 01:54

    Other solution is save the value of variable context $this inside other for example in this

    and for use is this.anyFunction();

    class MyClass {
        myTest() {
          console.log('it works');
        }
    
        runMyTest() {
          let this=$this;
          this.myTest();
        }
    }
    
    0 讨论(0)
  • 2021-02-05 01:59

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

    0 讨论(0)
  • 2021-02-05 02:00

    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

    0 讨论(0)
提交回复
热议问题