I'm struggling to grasp how I can access a this
that is located within one of the methods in an object literal that I have defined.
Let's take a look at an example:
var app = {
click: function(e) {
e.preventDefault();
console.log('Clicked');
this.saywhat();
},
saywhat: function() {
console.log('Say what ?');
}
};
var link = document.querySelector('a');
link.addEventListener('click', app.click);
When I click on the link, I encounter the following error:
Uncaught TypeError: this.saywhat is not a function
. How can I ensure that saywhat()
is accessible from within click()
? (I am looking for a solution where I don't have to use app.saywhat()
)