I have two classes which are not related:
class A {
myVar = null;
myFunc() {
console.log(this.myVar)
}
}
class B {
constructor(func){
this.func = func;
}
callFunc() {
this.func();
}
}
My problem is the following:
let a = new A();
let b = new B(a.myFunc);
b.callFunc();
This will be undefined
because this
in console.log(this.myVar)
won't refer to the instance of class A
, but the instance of class B
. How can I access the global variable of class A
when calling the function from another class?