Isn't it possible to create the different objects in a loop?
For example:
function theObj(strVal){
this.a =3D strVal;
}
var arrAllObj =3D new Array();
function createObj(){
var args =3D createObj.arguments;
for(var i=3D0; i<args.length; i++){
var oNewObj =3D "new theObj('" + args[i] + "')";
arrAllObj.push(eval("theObj" + args[i] + " =3D " + oNewObj));
}
}
createObj("Microsoft", "Netscape", "Opera");
alert(theObjMicrosoft.a);
Try this out, and see if it helps you!
/Robert
-----Original Message-----
From: Hans J=F6rg Friedrich [mailto:hans.friedrich@b...]
Sent: den 30 augusti 2001 11:56
To: javascript
Subject: [javascript] AW: RE: invoking object methods within a timeout
Thanks for your answer Robert.
Unfortunately (you were right), it's a bit more complicated than in the
example I posted with my question. The whole application has to be some
sort of "threadsafe". There can be as many objects of this type at a
time as you like and everyone of them can have completely different
parameters. Therefore I can't use global variables.
Any ideas?
Hans
-----Urspr=FCngliche Nachricht-----
Von: Nyman, Robert [mailto:Robert.Nyman@i...]
Gesendet: Montag, 27. August 2001 13:25
An: javascript
Betreff: [javascript] RE: invoking object methods within a timeout
I believe that when you come across this unfortunate situation,
you need to create a global variable that points to the object, and
then alert the object's property...
E.g. var oObjectOne =3D new theObject();
setInterval("alert(oObjectOne.a)", 1000);
/Robert
-----Original Message-----
From: Hans J=F6rg Friedrich [mailto:hans.friedrich@b...]
Sent: den 27 augusti 2001 11:55
To: javascript
Subject: [javascript] invoking object methods within a timeout
Hello,
I want to use a timeout (an interval exactly speaking) within a member
function of an object. The the code to be executed (the first parameter
of the setInterval function) is an invokation of another method of the
same object.
My problem is, when the method is called from the timer, 'this' points
to the window, not to the object like it should.
// just an example
function theObject()
{
this.a =3D 100
this.b =3D 2;
}
function addValues() // just to have a function to call
{
alert("in addValues!\n" +
"this.a:\t" + this.a +
"\nthis.document.URL:\t" + this.document.URL );
clearInterval(Timer); // comment this line if you don't see the
second alert
}
function addPerSecond()
{
try{
right =3D this.a;
wrong =3D this.document.URL;
}catch(e){
alert("in addPerSecond!\n" + e.message);
};
Timer =3D window.setInterval("this.addValues()", 1000);
}
var anObject =3D new theObject();
var Timer;
theObject.prototype.addValues =3D addValues;
theObject.prototype.addPerSecond =3D addPerSecond;
anObject.addPerSecond();
Would be great if anyone could help.
Hans