TS / JS: Overwrite a function and call original function within

前端 未结 2 519
感情败类
感情败类 2021-01-07 00:38

I\'d like to know if there\'s a way to modify a function in TypeScript and access the original function within. This is an example of how I got it to work:

le         


        
2条回答
  •  灰色年华
    2021-01-07 01:34

    When you assign a new function to an existing object that like you lose the reference to the old value.

    But if you use classes, you do have have access to overloaded methods via super.

    class ObjA {
        shout() {
            console.log('AHHHHH!')
        }
    }
    
    class ObjB extends ObjA {
        shout() {
            super.shout()
            console.log("I'm going to shout.")
        }
    }
    
    const obj = new ObjB()
    obj.shout() //-> "I'm going to shout", "AHHHHH!"
    

提交回复
热议问题