Interpreting String as function name actionscript 3.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfsiddiq
    New Member
    • Aug 2007
    • 17

    Interpreting String as function name actionscript 3.0

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

    Code:
    public var actionRef:String = "getit"
    
    public function initApp():void
    {
    button.label="Click"
    canvas.addChild(button);
    button.addEventListener(MouseEvent.CLICK,callMe);
    }
    
    public function callMe(event:MouseEvent):void
    {
    //if (this.hasOwnProperty(actionRef))
    this[actionRef]();
    
    }
    public function getit():void
    {
    trace("inside getit")
    }
    Cheers
    mfsiddiq
  • bartclarkson
    New Member
    • Feb 2008
    • 1

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

    Code:
    //  inside class containing function targeted by your string...
    
    public function executeThis(myFunctionStringName:String, someID:uint):void {
         var thisSoonToBeFunction:String = myFunctionStringName;
         this[thisSoonToBeFunction](someID);
    }
    					
    public function nameOfMyFunction(someID:uint):void {
       trace(someID);
    }
    
    public function triggerIt() {
        executeThis("nameOfMyFunction", 2);
    }

    Comment

    • yozef
      New Member
      • Oct 2009
      • 1

      #3
      Nope - Dynamic call of Function name

      So I have something like this:

      Code:
      for (var i:int=0; i<menu_array.length(); i++) {
      		var _callBackFuntion:String = "openMenu" + i;
      		menu_array[i].addEventListener(MouseEvent.CLICK, _callBackFuntion);
      	}
      Your system of using:

      Code:
      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?

      Comment

      Working...