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

This entry was posted in functions, return values. Bookmark the permalink. Both comments and trackbacks are currently closed.