The value of this within the handler using addEventListener
You need to "bind" handler to your instance.
var _this = this;
function onClickBound(e) {
_this.handleCellClick.call(cell1, e || window.event);
}
if (cell1.addEventListener) {
cell1.addEventListener("click", onClickBound, false);
}
else if (cell1.attachEvent) {
cell1.attachEvent("onclick", onClickBound);
}
Note that event handler here normalizes event
object (passed as a first argument) and invokes handleCellClick
in a proper context (i.e. referring to an element that was attached event listener to).
Also note that context normalization here (i.e. setting proper this
in event handler) creates a circular reference between function used as event handler (onClickBound
) and an element object (cell1
). In some versions of IE (6 and 7) this can, and probably will, result in a memory leak. This leak in essence is browser failing to release memory on page refresh due to circular reference existing between native and host object.
To circumvent it, you would need to either a) drop this
normalization; b) employ alternative (and more complex) normalization strategy; c) "clean up" existing event listeners on page unload, i.e. by using removeEventListener
, detachEvent
and elements null
ing (which unfortunately would render browsers' fast history navigation useless).
You could also find a JS library that takes care of this. Most of them (e.g.: jQuery, Prototype.js, YUI, etc.) usually handle cleanups as described in (c).
this keyword inside addEventListener callback
While we know that event listeners are executed with 'this' set to the event target, the below lines of code inside the EventTarget.prototype.dispatchEvent method in the EventTarget link that you found will answer your question as to how it is implemented.
for (var i = 0, l = stack.length; i < l; i++) {
stack[i].call(this, event);
}
The 'stack' array has the callback functions and they are invoked using .call by passing in the event target instance (this) and event as arguments.
What is this refer to in JavaScript?
Functions can be given a different this
scope to what would be the default. You can do this yourself with the apply
method. (Edit: as @pilchard pointed out below, this doesn't apply to arrow functions, which inherit the parent scope.) In event listeners, the this
refers to the element the event listener was applied to (currentTarget
).
Reference: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#the_value_of_this_within_the_handler
How to return value from addEventListener
Where you have the comment, you will never be able to access the variables, the event has not occurred yet.
Instead, what you can do is pass an anonymous function to the event handler, call your method which returns a value and use it as appropriate
function init()
{
var canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", function(event){
var result = getPosition(event);
// result is your return value
}, false);
}
Related Topics
What's the Significant Use of Unary Plus and Minus Operators
How to Fire and Forget a Promise in Nodejs (Es7)
Using "Object.Create" Instead of "New"
Youtube Iframe API: How to Control an Iframe Player That's Already in the HTML
Change Url Parameters and Specify Defaults Using JavaScript
Getting Value of Select (Dropdown) Before Change
How to Wait for Set of Asynchronous Callback Functions
Allow Google Chrome to Use Xmlhttprequest to Load a Url from a Local File
How to Check If a Checkbox Is Checked
How to Access Iframe Elements with JavaScript
How to Escape Regular Expression Special Characters Using JavaScript
Differencebetween String Primitives and String Objects in JavaScript
How Does JavaScript's Sort() Work
Insert HTML at Caret in a Contenteditable Div
Webdriver Click() VS JavaScript Click()
Why Doesn't My Equality Comparison Using = (A Single Equals) Work Correctly