473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem with "this" and context

I am having a problem with the use of "this". Maybe someone can help
me out here, let me know if I am doing this wrong:
Lets say I have an object MyObject, which has local variables, and some
methods. One of the local variables is an HTML input box. On
mousedown I want to execute some code like this:
function MyObject(inputFieldID){
// get the HTML element
this.InputField = document.getElementById(inputFieldID);
// local variable
this.MyID = inputFieldID;
// function trying to run
this.MyFunction = function(){
alert(this.MyID);
}

this.InputField.onkeydown = this.MyFunction ;

}
Now when you keydown inside the input textbox it errors because inside
the MyFunction function "this" doesn't refer to the object MyObject,
but rather to the Input element.

The only workaround I have found is to give the InputField a reference
to its owner so you have:

this.InputField.MyObject = this;

And then when your MyFunction becomes:
this.MyFunction = function(){
alert(this.MyObject.MyID);
}
That works, but it seems like a pain to have to carry those references.

Is there another way to do this?

Aug 8 '05 #1
2 1622
"cmay" <cm**@walshgroup.com> writes:
I am having a problem with the use of "this". Maybe someone can help
me out here, let me know if I am doing this wrong: Lets say I have an object MyObject, which has local variables, and some
methods.
Objects don't have local variables, so I guess you just mean
properties.
One of the local variables is an HTML input box.
A DOM HTMLInputElement object.
function MyObject(inputFieldID){ .... this.MyFunction = function(){
alert(this.MyID);
} this.InputField.onkeydown = this.MyFunction ; ....
Now when you keydown inside the input textbox it errors because inside
the MyFunction function "this" doesn't refer to the object MyObject,
but rather to the Input element.
Yes. The "this" operator refers to the base object of the property
reference that was called. When the onkeydown property of the input
element is called, "this" will refer to the input element during
the execution of the body of the function. That means that
alert(this.MyID)
will alert the undefined value.
The only workaround I have found is to give the InputField a reference
to its owner so you have:

this.InputField.MyObject = this;
Refernces between DOM objects and other objects is known to cause
memory leak in IE. You are already doing that, but no need to compound
the problem :)
And then when your MyFunction becomes:
this.MyFunction = function(){
alert(this.MyObject.MyID);
} Is there another way to do this?


Try:

var self = this;
this.MyFunction = function() {
alert(self.MyID);
}

The "self" variable will copy the refrence to the MyObject instance,
so that it is available to the function.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 9 '05 #2
Great, thanks.

Aug 10 '05 #3

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

Similar topics

1
by: lawrence | last post by:
I'm trying to gain a better understanding of javascript by studying examples. I noticed this in an online tutorial. I don't get the use of "this". >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> You might want...
9
by: aden | last post by:
I have read the years-old threads on this topic, but I wanted to confirm what they suggest. . . Can the this pointer EVER point to a type different from the class that contains the member...
5
by: ChrisB | last post by:
Hello: An object that is a field in another object has a constructor that requires a reference to the containing object: // object fields ChildObject childObject = new ChildObject(this); ...
2
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
1
by: Peter Rilling | last post by:
Hi. I am using Atlas and hooking a load event using add_load(...). This is being called from the constructor of an object being created, so it is within the context of an object (where 'this'...
5
by: Polaris431 | last post by:
I notice that the Dataset designer generates code for properties that look like this: public string ID { get { try { return ((string) (this)); } catch (System.InvalidCastException e) {
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
6
by: andrew.bell.ia | last post by:
Hi, I've got this very simple code that I don't understand (output follows code). Why does access to the _field variable fail without the "this"? I thought that once _field was added to the...
11
by: moltendorf | last post by:
Alright, another question to pin on the experts. :D I have an issue with "this" keyword in this case. The way I got around it before was to use a window property, but in this case I'm trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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...
0
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
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
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,...

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.