Yes, inside the callback this
refers to the element not the context of your instance. so try caching this
.
var Tset = function () {
var self = this; //<-- cache this to be used later
this.a = $('div').text("lorem ipsum ..").appendTo('#a1');
$(this.a).mouseover(function () {
self.setBackground('red'); //invoke it with self
});
this.setBackground = function (_color) {
$(this.a).css({
'background-color': _color
});
}
}
var x = new Tset();
There are other techniques available similar to this, that are using Ecmascript5 function.bind, $.proxy etc.
Using bound function:
var Tset = function () {
this.a = $('div').text("lorem ipsum ..").appendTo('#a1');
$(this.a).mouseover((function () {
this.setBackground('red'); //Now this will be your instanc's context
}).bind(this)); //bind the context explicitly
this.setBackground = function (_color) {
$(this.a).css({
'background-color': _color
});
}
}
Except for the bound functions, caller determines the context inside the callback
Fiddle