-1
export class MyClass {
myFuncA(msg){console.log('A: '+msg); console.log('this: ',this);}
myFuncB(msg){console.log('B: '+msg); console.log('this: ',this);}
}
我有一個名爲mc
的類實例;我需要一種方法來執行一個Typescript
類的動態命名函數。Typescript:如何爲動態執行的函數提供參數
此執行正常工作:
export class CallingClass {
constructor(){
let mc = new MyClass();
mc['myFuncA'].call(); // outputs 'A: undefined' 'this: undefined'
}
}
我有3個問題:
- 我怎樣才能提供論據?
mc['myFuncA'].call('message')
不起作用 - 如何將
this
設置爲CallingClass
?mc[...].call(...).bind(this)
錯誤 - 如何將
this
設置爲MyClass
?
讀取文檔:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call –
@BryanChen謝謝布萊恩,這很有幫助。不知道爲什麼我認爲這會很複雜。你是否在意足夠寫一個解決方案,以便我可以選擇它?否則,我會寫我自己的。 – BeetleJuice