In the constructor of my object, I create some span tag and I need to refers them to a method of the same object.
Here is an example of my code:
$(do
One simple way to achieve that:
function myObject(data){
this.name = data;
// Store a reference to your object
var that = this;
$("body").append("<span>Test</span>");
$("span").click(function(){
that.myMethod(); // Execute in the context of your object
});
this.myMethod = function(){
alert(this.name);
}
}
Another way, using $.proxy:
function myObject(data){
this.name = data;
$("body").append("<span>Test</span>");
$("span").click($.proxy(this.myMethod, this));
this.myMethod = function(){
alert(this.name);
}
}