I have the following example, and I want to access foo
variable within the function a
of my object test
.
class bar {
private foo: string = "foobar";
constructor() { /* ... Implementation ... */ }
fncA(): this {
// ... implementation
console.log(this.foo); // this is working as expected: "foobar"
const test = {
"a": function() {
// ... implementation
},
"b": function() {
console.log(this.foo); // TypeError: this.foo is undefined
}
};
return this;
}
}
TypeError: this.foo is undefined
In my opinion these are some binding problems of the this
variable, but I have no idea to solve this issue the right way.
I appreciate any help, thanks in advance.