Connecting Tech Pros Worldwide Help | Site Map

Interpreting String as function name actionscript 3.0

Newbie
 
Join Date: Aug 2007
Posts: 17
#1: Nov 21 '07
Hi
I want to know if there is any way a string can be interpreted as function name.I need this to call functions dynamically.Below is a code snippet which works fine if the function is in the same class as the function call.But what if i want to call a function present in another class or a different library.Any suggestions will be appreciated.

Expand|Select|Wrap|Line Numbers
  1. public var actionRef:String = "getit"
  2.  
  3. public function initApp():void
  4. {
  5. button.label="Click"
  6. canvas.addChild(button);
  7. button.addEventListener(MouseEvent.CLICK,callMe);
  8. }
  9.  
  10. public function callMe(event:MouseEvent):void
  11. {
  12. //if (this.hasOwnProperty(actionRef))
  13. this[actionRef]();
  14.  
  15. }
  16. public function getit():void
  17. {
  18. trace("inside getit")
  19. }
Cheers
mfsiddiq
Newbie
 
Join Date: Feb 2008
Posts: 1
#2: Feb 26 '08

re: Interpreting String as function name actionscript 3.0


Here is the code. I use this strategy for dynamic assignment of functions to events, like nav items. Enjoy.

Expand|Select|Wrap|Line Numbers
  1. //  inside class containing function targeted by your string...
  2.  
  3. public function executeThis(myFunctionStringName:String, someID:uint):void {
  4.      var thisSoonToBeFunction:String = myFunctionStringName;
  5.      this[thisSoonToBeFunction](someID);
  6. }
  7.  
  8. public function nameOfMyFunction(someID:uint):void {
  9.    trace(someID);
  10. }
  11.  
  12. public function triggerIt() {
  13.     executeThis("nameOfMyFunction", 2);
  14. }
Newbie
 
Join Date: Oct 2009
Posts: 1
#3: Oct 16 '09

re: Interpreting String as function name actionscript 3.0


So I have something like this:

Expand|Select|Wrap|Line Numbers
  1. for (var i:int=0; i<menu_array.length(); i++) {
  2.         var _callBackFuntion:String = "openMenu" + i;
  3.         menu_array[i].addEventListener(MouseEvent.CLICK, _callBackFuntion);
  4.     }
Your system of using:

Expand|Select|Wrap|Line Numbers
  1. menu_array[i].addEventListener(MouseEvent.CLICK, this[_callBackFuntion]);
does Not Work, it gives me a type error: Error #1006: value is not a function.

Anybody got other ideas?
Reply