Functions from arrays

Hello,
I have 5 functions.
I want one pulled out of an array and performed at random.

How could I code this? //im only using 2 funcs for space …


var way:Array = new Array("null","left")
var orientation:Number;
function null() {};
function left() {};
function direction()
{
orientation = Math.round(Math.random()*1);
way[orientation]; //gets the string > but how would I call it as a function!?
}
direction();

Thanks!!!

If you’re in a scope where this is accessible, then you could use:

this[way[orientation]]();

However, I think that it makes more sense to avoid using strings:

var way:Array = new Array(_null, left);
var orientation:Number;
function _null() {
}
function left() {
}
function direction() {
	orientation = Math.round(Math.random() * 1);
	way[orientation]();
}
direction();

Also, you may want to avoid using null as a function name because it is also the special value null.

thanks - avoiding strings for function calls was the key for me. :slight_smile:
I also tried looking at Senocular’s class Function Array which is pretty good…