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:
--------- - 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);
-
}
and here is how I'm using the above (using JQuery) - $(document).read(function(){
-
var t=new TestObj(50,100);
-
$("#tarea1").blur(t.display);
-
});
But when I do the following using Object literal , things seem to work
OK
METHOD B
-------- - 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));
-
});
Amazingly following also works :)
METHOD C:
--------- - function TestObj(){
-
-
}
-
-
TestObj.prototype.display=function(src,dest){
-
-
return function(){
-
alert("Source ="+this.src+", Destination ="+this.dest);
-
}
-
-
}
and here is how I'm using the above (using JQuery) - $(document).read(function(){
-
-
var t=new TestObj();
-
-
$("#tarea1").blur(t.display(5,10));
-
-
});
-
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
2 1241
On Jun 30, 11:16 am, Oltmans <rolf.oltm...@g mail.comwrote:
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:
--------- - 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);
-
-
}
and here is how I'm using the above (using JQuery) - $(document).read(function(){
-
var t=new TestObj(50,100);
-
$("#tarea1").blur(t.display);
-
});
I'm not a jQuery user but my guess is that "this" is not being set
properly when the "display" function is called. I'd guess that jQuery
set's "this" to be the DOM element that has blurred when the event
fires. You want "this" to be the "t" object. Perhaps try the
following. - $(document).read(function() {
-
var t=new TestObj(50,100);
-
$("#tarea1").blur(function() {t.display();});
-
});
But when I do the following using Object literal , things seem to work
OK
METHOD B
-------- - 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));
-
-
});
I wouldn't have thought that the above would work. do you have "src"
and "dest" attributes set on the tarea1 element or global variables
also?
Amazingly following also works :)
METHOD C:
--------- - function TestObj(){
-
-
}
-
-
TestObj.prototype.display=function(src,dest){
-
-
return function(){
-
alert("Source ="+this.src+", Destination ="+this.dest);
-
}
-
-
}
-
-
and here is how I'm using the above (using JQuery)
-
-
$(document).read(function(){
-
-
var t=new TestObj();
-
-
$("#tarea1").blur(t.display(5,10));
-
-
});
Again something seems odd here. There is never any setting of the
this.src and this.dest properties so how do they print properly?
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.
I imagine that jQuery has other workarounds for the IE memory leak
issues. Most event libraries do some clean up when the page unloads
and provide an API for "cleaning" elements of all their event
handlers.
Any ideas on how to make METHOD C work while avoiding a closure.
When registering event handler functions there is almost always
closures involved somewhere so the event handler function either knows
the correct "this" value or has access to other values it needs. The
problem with memory leaks is if the event handler function has a
reference to the DOM element the function observes. It doesn't look
like you have that problem in Method C.
How I might write this... - var handlerGenerator = function(src, dest) {
-
return function() {
-
alert("Source =" + src + ", Destination =" + dest);
-
};
-
};
-
-
$(document).read(function(){
-
$("#tarea1").blur(handlerGenerator(5,10));
-
});
-
Peter
- var t=new TestObj(50,100);
-
$("#tarea1").blur(t.display); <-- here your passing the function, no
-
way jQuery knows about 't' object
What you want to do is, - $("#tarea1").blur(function(){t.display();});
This is not jQuery fault, this is how JS works.
or you could do something like: - function TestObj(src, dest){
-
var me=this; // save what 'this' is pointing to, so you can use it
-
on a closure
-
me.src = src;
-
me.dest= dest;
-
me.count = 500;
-
-
me.display=function(){
-
//following displays 'undefined'
-
alert("Source ="+me.src+", Destination ="+me.dest);
-
//following also displays 'undefined'
-
alert('Count ='+me.count);
-
};
-
};
-
-
var t=new TestObj(50,100);
-
$("#tarea1").blur(t.display);// <-- should work now, here me!=this
-
but me==t
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alex |
last post by:
could somebody tell me the difference between those two styles:
function abc(var1, var2){ /* logic in here */ }
and
abc = function(var1, var2){ /* logic in here */ };
When / why would I use one over the other?
|
by: gor |
last post by:
I would like to find out how javascript can change some text (string)
inside <DIV> tag with event onClick.
That i want to solve with function callWrite(); ... so you can see in
the code. The thing should be simple, not to me ofcourse.
I dont have a clue, but maybe method getElementById may have somthing to
do with solving my problem. I dont know.
html>
|
by: Jon Slaughter |
last post by:
I have a class that is basicaly duplicated throughout several files with
only members names changing according to the class name yet with virtually
the exact same coding going on.
e.g.
class A
{
std::vector<B*> Bs;
public:
|
by: herrcho |
last post by:
#include <stdio.h>
int main()
{
int i;
char *p="sarang";
for(i=0;i<4;i++)
printf("%c",*(p+i));
printf("\n");
for(i=0;i<4;i++)
|
by: kobu.selva |
last post by:
I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.
I was always under the understanding that only "named" storage
areas(from the standard) were data objects. Since constants and string
literals don't have lvalues, they can't be data objects. Yet, I was
shown the first page of chapter 2 in K&R2, which states that variables...
| |
by: JJA |
last post by:
I'm looking at some code I do not understand:
var icons = new Array();
icons = new GIcon();
icons.image = "somefilename.png";
I read this as an array of icons is being built.
An element of the array is an object itself but what is this syntax of
the consecutive double quotes inside the brackets ?
|
by: SasQ |
last post by:
Hello.
I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?
I have read that literal constants may not to be allocated
by the compiler. If the Standard is saying that "object is
a region of storage", I deduce from that that literal constants
aren't objects because they may not be alocated as regions of
storage in the memory.
|
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire |
last post by:
Hello all,
I'm learning C and I still am struggling to understand some basic
concepts. For example, I read in the standard that with functions such
as strcpy, 'If copying takes place between objects that overlap, the
behavior is undefined.' But how can I be sure that they don't overlap?
For example, if I write this:
char string1 = "overlap";
|
by: minseokoh |
last post by:
Hi,
Could someone explain why "const" is located after the function name
and what it means?
inline unsigned char *access(int off) const {
if (off < 0)
abort();
return (&bits_);
}
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |