Maintaining the value of 'this' when using event listener
问题 I have the following function: onDocumentKeyUp: function (e) { if (e.keyCode === 27) { this.deactivate(); } } I wanted to bind this like so: $(document).on('keyup', onDocumentKeyUp); However, when I do that, the this inside the onDocumentKeyUp function refers to the document. I solved this by doing: var self = this; $(document).on('keyup', function(e) { self.onDocumentKeyUp(e); }); This works, but something tells me there's a cleaner way to pull this off. Would .call() or .apply() somehow.