Passing parameters to event listeners / handlers

前端 未结 8 1361
清酒与你
清酒与你 2020-12-06 06:40

How do you pass parameters / variables through event listeners? I\'ve overcome this problem fairly effectively using anonymous functions; which is an incredibly simple solut

8条回答
  •  孤城傲影
    2020-12-06 07:09

    You can do the following, which basically is a cleaner way of passing custom data.

    example code:
    public function myCustomEvenListener(e:MouseEvent,myCustomData:Object)
    {
    //Do whatever you wnat with myCustomData Object here...
    }
    //this is the function where you add event listeners to components..
    public function init():void
    {
    var myCustomObject:Object=new Object();
    myCustomObject.url="http://xyz.com/image.jsp";
    myCustomObject.anydata="You can Add anything here";
    
    mycomponent.addEventListener(MouseEvent.CLICK,function (e:MouseEvent) : void {
                      myCustomEventListener(e,myCustomObject);
                });
    
    }
    

    This is just another way of using anonymous functions though....

提交回复
热议问题