I've seen many answers of how you can access an object's properties, but not how to access them from a function of that object. So my question is:
- How do you access a property on an object, from a function, of a function of that object?
As an example, check out the code below...
// Wanted output:
1. Original settings // (a String)
2. Original settings // (a String)
Here's the code:
class Animal {
constructor(settings) {
this.settings = settings;;
}
changeSettings(newSettings) {
console.log("1. ", this.settings); // Prints OK.
checkOldSettings();
function checkOldSettings() {
console.log("2. ", this.settings); // This causes an error, as 'this' is undefined.
}
}
}
//==============================================================================
class Dog extends Animal {
}
//==============================================================================
let dog = new Dog("Origingal settings");
dog.changeSettings("... Whatever settings, not important here...");
Cheers