Category Archives: functions

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, associative arrays | Leave a comment

Function Returns a Function

Actionscript:
  1. appendExclamation("actionsnippit")("dot")("com")("is")("live");
  2.  
  3. function appendExclamation(str:String):Function{
  4.   trace(str + "! ");
  5.   return appendExclamation;
  6. }
  7.  
  8. /* outputs:
  9. actionsnippit!
  10. dot!
  11. com!
  12. is!
  13. live!
  14. */

This technique allows you to call a function more than once on one line. I first saw this used in the source files from a talk at flashcoders ny... I didn't attend the talk, but stumbled on this interesting blog post awhile back http://www.flashcodersny.org/wordpress/?p=166.

This is a fun technique to use, I've found some unusual uses for it already... some of which you'll see in later posts....

Also posted in return values | Comments closed