function Cons(name) {
var pvar1="hi "+name;
sayhi = function() {
alert(pvar1);
}
attEvents(){
$('#'+name).bind("click",sayhi);
}
}
var a = new Cons('name1');
var b = new Cons('name2');
var c = new Cons('name3');
Let us assume that name1, name2, name3 are all divs. Now, whatever div I click, I am always getting “hi name3”. The private variable is having the last stored value in it irrespective of the div clicked. Any help is appreciated.
Thanks
If you define it without
varkeyword then it is defined as a global. Thussayhirefers to the function defined as a last one. Useor
Note the comma. Or true jQuery style