Typescript: How to call method defined with arrow function in base class using super keyword in child class?

前端 未结 4 1200
青春惊慌失措
青春惊慌失措 2021-01-04 12:56

Given

class BaseClass{

  count:number=0;

  public someMethod=():void =>{
      this.count++;
  }
}

class ChildClass extends BaseClass{
  public someMe         


        
4条回答
  •  囚心锁ツ
    2021-01-04 13:46

    Without a function implementation in the prototype, there's no way for the derived class to 'find' the base class implementation. You can separate it out so that you have one method for preserving this and another for using via super:

    class BaseClass {
      count: number = 0;
    
      someMethodImpl() {
        this.count++;
      }
    
      public someMethod = this.someMethodImpl;
    }
    
    class ChildClass extends BaseClass {
      public someMethod = (): void=> {
        super.someMethodImpl();
        //Do more work here.
      }
    }
    

提交回复
热议问题