473,508 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Events, Objects and the 'this' object

Here's a good one,

I have an object class wich dynamically creates a control in the page.
Then it binds an event to that control, pointing on one of its methods
(the object's). This method, in turn, calls other of its own
methods...

The binding seems ok and the change() method does get called. But I
get an error message (This object does not support this property or
method) when I call other methods on the object.

I think that the implicit "this" object sent to the method does not
point to the right object instance...

Does anyone have a clue on how I might solve this ?

TextField.prototype.generateField = function() {
this.fieldReference = window.document.createElement("input");
this.fieldReference.type = "text";

this.fieldReference.name = this.fieldId;
this.fieldReference.id = this.fieldId;
this.fieldReference.value = this.value;

this.fieldReference.attachEvent("onchange", this.change);

this.fieldWrapperReference.innerHTML = "";
this.fieldWrapperReference.appendChild(this.fieldR eference);
}

TextField.prototype.change = function() {
// Fired by the onchange event
this._setStyles();
this._updateChilds();
if (this.onChangeFunction != null) {
this.onChangeFunction();
}
}

Jun 9 '06 #1
5 1198
DBoy001 wrote:
Here's a good one,
I think that the implicit "this" object sent to the method does not
point to the right object instance...

Does anyone have a clue on how I might solve this ?

TextField.prototype.generateField = function() {
this.fieldReference = window.document.createElement("input");
this.fieldReference.type = "text";

this.fieldReference.name = this.fieldId;
this.fieldReference.id = this.fieldId;
this.fieldReference.value = this.value;

this.fieldReference.attachEvent("onchange", this.change);


Hmmm let me think....

var self = this;
var innerFunc = new function()
{
self.change();
}
this.fieldReference.attachEvent("onchange", innerFunc);

Does this work?
Jun 9 '06 #2
DBoy001 wrote:

Hi,
I think that the implicit "this" object sent to the method does not
point to the right object instance...
That's correct.
this.fieldReference.attachEvent("onchange", this.change);


Here, you attach the function "this.change" and not the method
"this.change". This means that the "this" value inside of "this.change"
depends on the caller, and would be in your example the element from
which the event is generated, and not your custom object.

You have many ways to solve this issue, but you'll have probably to
refine a bit your object interface, thinking about how to grasp/hold
references. One of the most appropriate possibility is
"associateObjWithEvent" pattern, which can be found in this newsgroup
FAQ Notes :

<URL:http://www.jibbering.com/faq/faq_notes/closures.html>
HTH
Jun 9 '06 #3
Robert wrote:
<snip>
Hmmm let me think....

var self = this;
var innerFunc = new function()
{
self.change();
}
At this point - innerFunc - has been assigned a reference to a newly
created object and the - self.change(); - method has been called. This
is unlikely to be desirable.
this.fieldReference.attachEvent("onchange", innerFunc);

Does this work?


No, - innerFunc - is not executable.

Richard.

Jun 9 '06 #4
Richard Cornford wrote:
Robert wrote:
<snip>
Hmmm let me think....

var self = this;
var innerFunc = new function()
{
self.change();
}

At this point - innerFunc - has been assigned a reference to a newly
created object and the - self.change(); - method has been called. This
is unlikely to be desirable.


I'm sorry. I meant to write
var innerFunc = function()
Jun 9 '06 #5
Thanks everyone for quick and smart answers, this has helped a lot.

Elegie: the article you provided
(<URL:http://www.jibbering.com/faq/faq_notes/closures.html>) offers
great solutions and has helped me comprehend a lot about the
function/object mechanism... thanks !

Jun 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4138
by: Tom Bean | last post by:
Can events be used to notify an object that something has happened and it needs to take some action? Most of the documentation on events makes them sound like they work the other way around,...
2
9150
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
15
2071
by: Rhy Mednick | last post by:
I have a class (let's call it ClassA) that I've written which has events. In another class (let's call it ClassB) I create a collection of ClassA objects. In a third class (ClassC) I create a...
4
5462
by: Ben | last post by:
Hello, Can anyone tell me what is difference between raising your own events and writing functions. I can do the same thing in a function, so why would I want to raise an event? I am not very...
3
2319
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
7
1803
by: Jay Douglas | last post by:
Greetings, I have a Windows form application that (naturally) instantiates all sorts of objects. I have a base object that contains an event. Lots of other objects inherit from this event. ...
3
2265
by: daan | last post by:
Hello, I have a problem and I can't get the solution for it :( I have a com dll, which i imported as a reference. The com object is part of a class which is multithreaded and will create...
11
3219
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
13067
ADezii
by: ADezii | last post by:
Not all ADO Power Users realize that there are Events, specifically related to the Connection and Recordset Objects, for which they can write code for. If one is aware of these Events, it is not...
3
9451
Frinavale
by: Frinavale | last post by:
Background An Event is a message sent out by an object to notify other objects that an action/trigger/state-change (ie. an event) has taken place. Therefore, you should use an event when an object's...
0
7323
Oralloy
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,...
0
7380
jinu1996
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...
1
7039
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...
0
7494
tracyyun
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...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
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...
0
1553
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.