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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With