I am looking to import another class, specified by a name string (for example, 'B'), into the current class (named 'A'). The function in class B needs to be able to call a function in class A. I am seeking guidance on how to achieve this.
// index.ts
function importAll(obj, src){
for (var key in src) obj[key] = src[key];
return obj;
};
class A{
function_a(){
console.log("A_test");
}
call_function_b()
{
let classtype="./Bclass";
// dynamically import class B
let B = require(classtype);
let object_b= new B();
// want to import all functions from class B to class A
// like importAll(this, object_b);
}
}
export let test =new A();
test.call_function_b();
//file Bclass.ts
export class B{
function_b() {
console.log("B_test");
}
function_use_a()
{
// function in class B needs to call function in class A
this.function_a();
}
}