473,387 Members | 1,703 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,387 software developers and data experts.

JScript objects, events and "this"

Hi ! I have a question about JScript :

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
2 1985
wrote on 09 jun 2006 in microsoft.public.inetserver.asp.general:
Hi ! I have a question about JScript :
[...]


Though ASP can be written in Jscript, it is and remains serverside.

Your questions are about j(ava)script and the DOM,
so in a clientside browser.

Off-topic on this classic ASP NG.

Try:

microsoft.public.scripting.jscript

or

comp.lang.javascript
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 9 '06 #2

<da********@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi ! I have a question about JScript :

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();
}
}

TextField.prototype.generateField = function()
{
var sInput
sInput = '<input type="text" ';
sInput += 'id="' + this.fieldId + '" ';
sInput += 'value="' + this.value + '" ';
sInput += 'onchange=TextField.prototype.change.call(this) '
sInput += '/>'

this.fieldWrapperReference.innerHTML = sInput
this.fieldWrapperReference.firstChild.myOwner = this
this.elemId = this.fieldWrapperReference.firstChild.uniqueID // if
fieldId is not unique across document

}
TextField.prototype.getElem = function()
{
return document.getElementById(this.elemId) //OR use fieldId is unique
across document
}
TextField.prototype._setStyles = function(elem)
{
if (!elem) elem = this.getElem()
// your code but instead of this.fieldReference use elem
}

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

Fundamentally to create a control like object around DHTML elements you will
need the element to reference the 'owning' Javascript object and the
Javascript object will need to hold a reference to the DHTML element.

This creates a circular reference which, under mozilla, is not a problem
since all objects Javascript and DHTML are handled the same way in a garbage
collecting space. However in IE the DHTML elements are COM objects so
circular references like this will result in memory leak. The solution is
to never hold a reference to a DHTML object in your javascript objects.
Hold a unique ID instead and use geElementById to retrieve the element when
needed.

BTW events created with attachEvents all execute with the this object being
window.

Anthony.
Jun 9 '06 #3

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

Similar topics

6
by: Mike Kamermans | last post by:
I'm trying to determine where inside an XHTML's DOM a javascript function is triggered, but when I use the construction of calling a function with "this" as parameter and then checking the event...
32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
2
by: cmay | last post by:
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...
4
by: Richard | last post by:
When I reference "this" in call to event, I get the following error: An unhandled exception of type 'System.NullReferenceException' occurred in csbasesockets.dll Additional information: Object...
7
by: Daniel Ervi | last post by:
Hi All, I have a question for the group as I can't seem to come up with any suitable solutions. I'm not that new to programming or C#, but neither am I very fluent yet, so I'd appreciate any...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using 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?
5
by: Jeremy | last post by:
Is there any good reading about pitfalls of scoping when using events? Here is my specific issue: function MyType() { this.foo = "bar"; this.textbox = document.createElement("input");...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.