I know there are many questions about this
, and I have read many answers and references, including this canonical answer. However, I am not able to derive a solution to my specific problem.
Here is a sample code:
class A {
myField;
constructor(myField) {
this.myField = myField;
}
myMethod() {
console.log(this.myField);
}
}
class B {
constructor(myFunc) {
myFunc();
}
}
const a = new A("Hello");
const b = new B(a.myMethod);
I get this error:
Cannot read properties of undefined (reading 'myField')
It seems that this
inside myMethod
is undefined
and does not refer to the instance of A
, as I would expect (coming from C#...).
What can I do to refer to myField
?