Home » AS3, flash, usefull

Call a function by its name in AS3

23 September 2009 2 Comments

Imagine you want to call a function but you don’t have its reference, only its name.
Well everything in ActionScript 3 follows pretty much the same logic. Every member of an object can be accessed via object["member_name"];

Now it should be easy to get a function from it:

var f:Function = object["function_name"] as Function;
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • TwitThis

2 Comments »

  • welwolfe said:

    This is exactly the line of code i need!
    Thank you!

    one little typo “object” should be “Object”

  • Matias said:

    “object” is fine, he meant the instance of the object in where the function is. It could be also “this”:

    var f:Function = this["function_name"] as Function;

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

Security Code: