Category Archives: associative arrays

Associative Array Function Call

Actionscript:
  1. for (var i:int = 0; i<4; i++){
  2.     this["phase"+i]();
  3. }
  4.  
  5. function phase0():void{
  6.     trace("phase 0");
  7. }
  8. function phase1():void{
  9.     trace("phase 1");
  10. }
  11. function phase2():void{
  12.     trace("phase 2");
  13. }
  14. function phase3():void{
  15.     trace("phase 3");
  16. }
  17.  
  18. /*
  19. will output:
  20. phase 0
  21. phase 1
  22. phase 2
  23. phase 3
  24. */
  25.  
  26. /*
  27. WARNING: This code was written for fun. Use at your own risk.
  28. */

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....

Also posted in arrays, functions | Leave a comment