I have a class and this class have a function. I want to call that function from inside another function that is inside an object inside of that class.
Here is the code:
class Animal
{
constructor(name)
{
this.name = name
}
//This is the function i want to call from inside the object below
emitSound(sound)
{
return `I am ${sound}`
}
//This is the object with functions
actions = {
eat()
{
return this.emitSound('eating');
},
sleep()
{
return this.emitSound('sleeping');
}
}
}
const animal = new Animal('Dog');
console.log(animal.actions.eat());
(this code is just an example, the application im developing is way more complex)
How you can see, it returns an error saying that "this.emitSound()" is undefined.
Its happening because the function eat() is trying to call this from inside the actions object, isnt it?
So what I need is a way to call the emitSound() function inside the actions object functions. Please help!!!