ActionScript 3 Call a Function by String Representation of Function Name


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * ActionScript 3 Call a Function by String Representation of Function Name
  3.  *
  4.  * This will allow you to execute a function if you only have the function name
  5.  * as a string. The compiler won't catch any issues with this, so be careful with
  6.  * it. However, it can save a lot of time and code if used correctly.
  7.  *
  8.  * The first argument to "apply" is the object that the function should apply to
  9.  * The second argument is an Array object of arguments to pass to the function
  10.  */
  11. var functionName:String = "myFunction";
  12. var containerObject:Object = myContainerObject;
  13.  
  14. if (containerObject.hasOwnProperty(functionName))
  15. containerObject[functionName].apply(null, [ args ]);
  16. else
  17. trace("Cannot call function: " + functionName + " does not exist in " + containerObject.id);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.