I have an constructor object like this .
var Cons = function(me){
this.name= me;
this.ele = document.getElementById("element");
this.ele.addEventListener("click",this.fnClick,false);
}
and some methods extended as prototypes
Cons.prototype.getme= function(){
return this.me;
}
Cons.prototype.fnClick= function(){
alert(this.getme())
}
var initZ = new Cons("test");
So my problem is when im “fcClick” is executed as a bind function “this” will refer to the binded HTML element , not the current instance Object,
So how can i pass the reference of “this” in “fnClick” ..?
jsfiddle link
Use
this.fnClick.bind(this)or, for browsers that do not support bind:See http://jsfiddle.net/Fy5ZV/1/ (with
getme()returningthis.name)