I have the following problem in event handlers in Javascript. I've got an object that has a mousemove
event handler like so:
function MyObject(){ }
function MyObject.prototype = {
currentMousePosition: null,
onMouseMove: function(ev){
this.currentMousePosition = this.getCoordinates(ev);
},
getCoordinates: function(ev){
if (ev.pageX || ev.pageY)
return { x: ev.pageX, y: ev.pageY };
return { x: ev.clientX + document.body.scrollLeft - document.body.clientLeft, y: ev.clientY + document.body.scrollTop - document.body.clientTop };
}
};
The problem I'm trying to solve resolves around object context. Within my onMouseMove
function it assigns the currentMousePosition
property. Naturally this won't work because it's a static function handling the mousemove
event.
What I'm looking for is a technique/method to pass an object context in with my event handler. The best example that I can think of is the Google Maps API function GEvent.bind()
.
With it you can pass the object with the function you want to fire on the specified event. I'm essentially looking for the same thing.