PDA

View Full Version : Converting strings to functions


ixlepixle
09-05-2004, 07:50 PM
Is there a way to convert a string-variable to a function call?
Consider this scenario:

I have the name of a function saved as a string, let us call it funcName.
How can I apply this function to an instance of a class that has a function with the same name?

Example:

class Test{
function myFunc(){
trace("whatever");
}
}

var myObj:Test = new Test();
var func = "myFunc";
// Trying to call the function
myObj.func();




I'm having trouble making this work.. anybody know how?:)

CyanBlue
09-05-2004, 08:28 PM
This works in FMX... :)

var func = "myFunc";
eval(func)();

function myFunc()
{
trace("whatever");
}

nunomira
09-05-2004, 09:47 PM
hi,

For more details read dynamic references (http://www.nunomira.com/tutorials/dynamic_references.php#functions)

agent81
09-06-2004, 03:36 AM
also you can



var func = "myFunc";
this[func]();

function myFunc()
{
trace("whatever");
}

deQue
09-06-2004, 08:53 AM
I use agent81's method most of the time, cause it's very intuitive, and follows the same method for refrencing child objects and arrays.