The code structre is quite simple:
class A {
b = 1
c = {
d () {
console.log(this.b) //=> undefined
//how can i access b here?
}
}
}
I would prefer a not so hacky workaround since this is a core piece of code for the project i am working on
CodePudding user response:
You could use an arrow function to preserve this
where the function is being declared:
class A {
b = 1
c = {
d: () => console.log(this.b)
}
}
const a = new A;
a.c.d();