PDA

View Full Version : How can I get instance name of variable after creating a customized class?


Cyman
10-13-2004, 07:08 AM
var myClassA = new ClassA();

After I created a myClassA variable of class "ClassA"...
In "ClassA",
How can I use "this.xxxxx", which xxxxx is a function or property, to get a string "myClassA" back?

How can I get instance name of variable after creating a customized class?

hemebond
10-13-2004, 07:29 AM
Why do you need to do this?

Cyman
10-13-2004, 07:36 AM
I wanna call "SetTimeout("myClassA.test()", 2000);" :D

glenngv
10-13-2004, 08:48 AM
You don't need to know the variable used. You can also pass an anonymous function in the setTimeout.

function ClassA(){
var me = this;
this.test = function(){
window.status++;
setTimeout(function(){me.test()}, 100);
}
}
var c = new ClassA();
c.test();

Cyman
10-13-2004, 09:10 AM
I think your coding works partially for my case...
Coz, I'm writing a background efffects creater now...

Also, I've uploaded my js and testing htm to this...
If you've any idea to work in my case, pls say it.

Thx Again! :thumbsup:

glenngv
10-13-2004, 09:27 AM
I didn't see you incorporate my solution to yours.

function JSBGEffect() { //no need for the varName
var me = this; //use me in all the public methods in this constructor
...
setTimeout(function(){me.lightingEffect()}, Math.round((Math.random() * me.lightingTimer)));
...
setTimeout(function(){me.snowingEffect()}, me.snowingSpeed);
...
}

Cyman
10-13-2004, 10:02 AM
I just use your method to change coding... but it causes some errors (that object doesn't support that method...)

maybe I used prototype function to contruct common function?

glenngv
10-13-2004, 10:09 AM
Can you attach the updated js code?