I am trying to call a function via reference. It all starts in callMe() of class DynamicCalls:
interface IDynamicFunction {
name: string;
func: (param: string) => void;
}
class DynamicCalls {
private dynamicCall: IDynamicFunction = { name: "myDynamic", func: this.testFunc };
public callMe() {
this.callFromDynamic("Works"); // 1st -> Works
this.testFunc("Works, also"); // 2nd -> Works
this.dynamicCall.func("First Call"); // 3rd -> Error: callFromDynamic seems to be unknown in testFunc
}
private callFromDynamic(param: string): void {
console.log("Param: " + param);
}
private testFunc(param: string): void {
console.log("Param: " + param);
this.callFromDynamic("Second call"); // Gives error -> TypeError: this.callFromDynamic is not a function
}
}
let dynamicCalls: DynamicCalls = new DynamicCalls();
dynamicCalls.callMe();
I expect that the 3rd call (this.dynamicCall.func("First Call");) works like this.testFunc("Works, also").
Can anyone explain to me why I get this TypeError: this.callFromDynamic is not a function? And how I can avoid it?
Thanks a lot in advance.
Kind regards, Okean