Code Snippets: Calling a Function from a String in Flash AS3

There are often odd cases when in a Flash Application you find that by design you want to call a function but only have the name of the function as a String. This could be caused by various reasons one being that the name is stored in a database as part of some settings. To accomplish this conversion we make use of the Function object.

How to Convert a String to a Function

var functionName:String = "toString";

// obtain the function handler
var namedFunction:Function = this[fName];

// execute the function and pass any desired arguments
nameFunction( arg1, arg2, ... );
// Code Snippets // Flash //

Comments & Questions

Add Your Comment