Here is what i cannot get to work – in mouseMove this.mouseX is undefined. I need the mouseX and mouseY (e.pageX in code) from mousemove event.
function myClass () {
var mouseX=x;
var mouseY=y;
$(document).mousemove(this.mouseMove);
}
myClass.prototype.mouseMove = function(e){
this.mouseX=e.pageX;//not a good idea but you can alert this
}
var a=new myClass();
Can i get that event passed to the supposed class instance or there is no way to get it done?
I should also mention i am trying to avoid global scope variables here.
Twothree things:var mouseXonly declares a variable local to the myClass function. You have to declare a property of the instance withthis.mouseX(thisrefers to the current instance):Secondly, make sure you pass
xandyas parameters to the constructor. Otherwise you will get an error (if they are not defined in higher scope, but that does not seem useful to me).Thirdly, if you just pass
this.mouseMoveas event handler, inside the event handler,thiswill refer towindowand not to themyClassinstance. Use$.proxyto set the context of the callback explicitly:I suggest to read MDC – Working with objects.