I have a scenario where I am trying to access the foo
variable inside the function a
of the test
object.
class bar {
private foo: string = "foobar";
constructor() { /* ... Implementation ... */ }
fncA(): this {
// ... implementation
console.log(this.foo); // This correctly outputs "foobar"
const test = {
"a": function() {
// ... implementation
},
"b": function() {
console.log(this.foo); // TypeError: this.foo is undefined
}
};
return this;
}
}
TypeError: this.foo is undefined
It seems like there are some binding problems with the this
variable. I am struggling to find the right solution to this issue.
Any help would be greatly appreciated. Thank you in advance.