Associative Array Function Call

for (var i:int = 0; i<4; i++){
	this["phase"+i]();
}

function phase0():void{
	trace("phase 0");
}
function phase1():void{
	trace("phase 1");
}
function phase2():void{
	trace("phase 2");
}
function phase3():void{
	trace("phase 3");
}

/*
will output:
phase 0
phase 1
phase 2
phase 3
*/

/*
WARNING: This code was written for fun. Use at your own risk.
*/

I was pleasantly surprised back in the AS1 days… when I discovered that associative array syntax worked along with function calls (why wouldn’t it?). There are some interesting tricks you can do with this technique…. will post some later….

Leave a Reply

Your email address will not be published. Required fields are marked *