I want to make a javascript object that can handle interaction with a headless CMS for me, but I am stuck at this simple problem. I cant figure out how to bind "this" the right way:
const car = {
model: "Volvo",
get: {
getModel() {
console.log("The car is: " + this.model);
}
}
}
car.get.getModel() // The car is undefined
When I simply log "this" inside getModel() I see that "this" refers to the get property. What am I doing wrong?
I have seen a solution where you use bind:
const getModel = car.get.getModel.bind(car);
but it seems like there should be a better way.
I created the object and tried using the method getModel(), but the way I structured it I can't get it to work.