An alternative solution, if you do not have Function.prototype.bind available *, and you do not want to add additional functions to Function.prototype , would close the call to this.bar :
function foo() { var self; self = this; window.addEventListener('keydown', function (e) { self.bar(e); }, false); } foo.prototype.bar = function (e) { console.log(e.keyCode); }
*, although using addEventListener without attachEvent makes me think that Function.prototype.bind would be an acceptable choice
In addition, libraries such as jQuery can contain their own bind form, jQuery jQuery.proxy .
source share