Hi all,
I'm stuck in a situation where I need help. Any help will be highly
appreciated. I've created an object, after creating the object, I'm
assigning one of its functions to an event handler i.e. to onBlur
event of Text-area. Problem is that when I try to access object
properties from the handler function, I cannot access them in some
cases and get a value of undefined. Here is the sample code that I'm
trying
METHOD A:
---------
and here is how I'm using the above (using JQuery)
But when I do the following using Object literal , things seem to work
OK
METHOD B
--------
Amazingly following also works :)
METHOD C:
---------
and here is how I'm using the above (using JQuery)
Actually, I want to go with the Function object approach mentioned
under METHOD C, but I'm also trying to avoid the closure in " display
" function as I've read on Internet that it can cause memory leaks in
IE6. Any ideas on how to make METHOD C work while avoiding a closure.
Also, if you can give a reason as to why METHOD A isn't working that
will be highly appreciated.
Thanks in advance.
Regards,
Oltmans
I'm stuck in a situation where I need help. Any help will be highly
appreciated. I've created an object, after creating the object, I'm
assigning one of its functions to an event handler i.e. to onBlur
event of Text-area. Problem is that when I try to access object
properties from the handler function, I cannot access them in some
cases and get a value of undefined. Here is the sample code that I'm
trying
METHOD A:
---------
Code:
function TestObj(src, dest){
this.src = src;
this.dest= dest;
this.count = 500;
}
TestObj.prototype.display=function(){
//following displays 'undefined'
alert("Source ="+this.src+", Destination ="+this.dest);
//following also displays 'undefined'
alert('Count ='+this.count);
}
Code:
$(document).read(function(){
var t=new TestObj(50,100);
$("#tarea1").blur(t.display);
});
OK
METHOD B
--------
Code:
var TestObj={
display:function(src,dest){
return function(){
alert("Source ="+this.src+", Destination ="+this.dest);
}
}
};
$(document).read(function(){
//This works and I see values of 50 & 100 in the alert box
$("#tarea1").blur(TestObj.display(50,100));
});
METHOD C:
---------
Code:
function TestObj(){
}
TestObj.prototype.display=function(src,dest){
return function(){
alert("Source ="+this.src+", Destination ="+this.dest);
}
}
Code:
$(document).read(function(){
var t=new TestObj();
$("#tarea1").blur(t.display(5,10));
});
under METHOD C, but I'm also trying to avoid the closure in " display
" function as I've read on Internet that it can cause memory leaks in
IE6. Any ideas on how to make METHOD C work while avoiding a closure.
Also, if you can give a reason as to why METHOD A isn't working that
will be highly appreciated.
Thanks in advance.
Regards,
Oltmans
Comment