Possible Duplicate:
How to get the this of a object in a handler for a click event in jquery?
I’m trying to create object that will bind few events to another object method. My code is some like this:
var MAP = MAP || {}
MAP = {
height: 100
/*
* rest of code
*/
};
var CONFIG = CONFIG || {}
CONFIG = {
_MAP : MAP,
setEvents:function(){
$('#map').mousedown(function(){
height = this._MAP.height;
console.log(height);
}
};
$(function(){ CONFIG.setEvents(); })
and i cant connect height in setEvents method with MAP.height, but when I call this._MAP.height directly from method (not bind to on mouse down) every thing is ok. Can someone help me resolve this problem?
In the function body for
.mousedownthisrefers to the DOMElement that$('#map')found.In order to keep track of the proper
this, you want to store it in your closure, outside the call to.mousedown.var $this = thisin thesetEventsfunction, for example.