TypeScript - how to inherit class and override lambda method

后端 未结 2 1768
醉酒成梦
醉酒成梦 2020-12-19 02:06

I have an inherited class, and need the parent to have a virtual method, which is overridden in the child class. This method is called from the base constructor, and needs a

2条回答
  •  我在风中等你
    2020-12-19 02:44

    Well, you can't have that.
    There's an issue that was opened but it was closed as "by design".

    You should use regular methods:

    class Base {
        protected prop = null;
    
        constructor() {
            this.init();
            this.initLambda();
        }
    
        init() {
            console.log("Base init");
        }
    
        initLambda() {
            console.log("Base initLambda");
        }
    }
    
    class Derived extends Base {
        constructor() {
            super();
        }
    
        init() {
            console.log("Derived init");
        }
    
        initLambda() {
            console.log("Derived initLambda");
        }
    }
    

    And then it will work.

    As for keeping the right this, you can always pass a call to the method as an arrow function:

    doit() {
        setTimeout(() => this.init(), 1);
    }
    

    Or use the Function.prototype.bind function:

    setTimeout(this.init.bind(this));
    

    Also, the _this thing that the typescript compiler produces is just a hack to polyfil the arrow functions for ES5, but if you change the target to ES6 then it won't use it.


    Edit:

    You can save the bound methods as members:

    class Base {
        ...
        public boundInit: () => void;
    
        constructor() {
            ...
            this.boundInit = this.initLambda.bind(this);
            setTimeout(this.boundInit, 500);
        }
    
    ...
    

    With that, when I do new Derived() this is what I get:

    Derived init
    Derived initLambda // after 200 millis

提交回复
热议问题