Click to See Complete Forum and Search --> : dynamic call for a class.function


jessevitrone
07-18-2003, 08:33 AM
Is there a way to make a dynamic call to a function that is on a class?


MyClass.prototype.setMainHTML = function() {
var displayFunc = new Function("setMainHTML" + this.attributes["type"] + "()");
displayFunc();
};


this.atributes["type"] == "Text"

I'd like to be able to call the MyClass.setMainHTMLText dynamically. I have a MyClass.setMainHTMLText defined, but I don't know how to make the call so that it's executing for the current object.

I tried making the string inside the Function() method "this.setMainHTML", but that didn't work. Neither did trying to use "this.displayFunc();"

Any suggestions?

Khalid Ali
07-18-2003, 08:44 AM
typicallu this is how it shoudl be done suppose a class
function MyClass(....){

}and this class has a method

setMainHTML..to call this method

get a reference of MyClass

var mc = bew MyClass(...)

now
mc.setMainHTML()

should do what you are trying to do...

jessevitrone
07-18-2003, 08:48 AM
I know, that's how I have most of my functions.

But my setMainHTML method has to do different things depending on what the "type" value is set to.

So instead of having a switch or big if / else that has to be added to everytime I define a new type, I was trying to dynamically call the proper function to handle that type.

So since this.attributes["type"] == "Text", I was trying to have setMainHTML call setMainHTMLText

Does that make sense?

I know everything else is set up right, because if I just change it to a hard-coded call to this.setMainHTMLText, it works.