I am probably doing something wrong but I found some interesting behavior when trying to apply some object oriented programming to Javascript. Consider the following
function Bug(element) {
this.focusedCell = null;
element.addEventListener('click', this.onClick, true);
};
Bug.prototype.onClick = function(event){
console.log("this is: ");
console.log(this);
};
When I call the method from the console, I see the correct instance of "this" but when I click the element in the document I see the document element in lieu of the instance. So... chances are it's probably not a good idea to use event listeners with instance methods, at least the way I'm doing it.
So the question is:
Is it possible to have an event listener like this that calls an instance method of a javascript object, while preserving the instance in the call?
Is there a better pattern for doing this?
Edit: I haven't tried this in anything but Chrome. But I would imagine that the behavior is the same.