I think that code is worth a thousand words. Take this example
class Cat {
constructor() {
this.meow("roar", this.sound)
}
meow(a, callback) {
callback(a)
}
sound(a) {
console.log(a)
console.log(this.sayMeow) <----- THIS IS UNDEFINED
}
sayMeow() {
return "Meow"
}
}
As you can see method sayMeow()
is undefined. Can you please explain why and how can i solve it?
This is just simplified representation of more complex code where i have to use callbacks. I need to know why method is undefined inside callback function. Please do not write modifications of this simple Cat class.
Thank you