JQuery seems to overwrite the this
keyword of my instance method, when added as an event handler. Why does this happen? Code below:
function Foo() {};
Foo.prototype.bark = function() {
console.log(this);
};
var foo = new Foo();
$("#b").click(foo.bark);
Output on click:
<button id="b" type="button">
However JQuery does not seem to override the this
keyword in the code below:
$("#b").click(function() { foo.bark(); });
Output on click:
`Foo { bark=function()}`