Call a function by its name in AS3

3 Flares Twitter 0 Facebook 0 Google+ 3 Email -- Filament.io 3 Flares ×

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:

[sourcecode language=’as3′]
var f:Function = object[“function_name”] as Function;
[/sourcecode]

3 Flares Twitter 0 Facebook 0 Google+ 3 Email -- Filament.io 3 Flares ×

3 Comments

  1. welwolfe

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

    one little typo “object” should be “Object”

  2. Matias

    “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;

  3. Thanks! exact what I needed!!

Leave a Comment