this points to the context. In event handlers, this is the element that received the event (the button, the div, the link...). If you call a function as if it were a method, this is the object. But the fact that the function is declared as a method does not ensure that it is called as such.
For example:
If you are in the global context (you are not inside any function), this is window
console.log(this===window); function test() { console.log(this===window); if (this!==window) { console.log(this); } } test(); this is window
But if that function is an attribute of an object, then this becomes that object:
function test() { console.log('Es window?:' + (this===window)); if (this!==window) { console.log('Es obj?:' + (this===obj)); } } console.log('Modo no estricto'); var obj={ prueba: 'hola' } test(); console.log('Como método'); obj.metodo=test; obj.metodo();