views:

2887

answers:

2
fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);

  private function uploadCompleteHandler(event:Event):void {}

Above is one way to add an event listener in Actionscript. By default the callback function needs to have an argument with name event and type Event. Is there a way to declare this function without any arguments :

  private function uploadCompleteHandler():void {}

Edit : It's possible to add an event handler without any arguments in mxml. So one student wanted to know, why isn't it possible to do the same in actionscript?

+3  A: 

No, you'll get a runtime exception when the event fires if you'll try to register it as event listener.

You may use the following syntax to allow calling your handler directly without event object:

private function uploadCompleteHandler(event:Event = null):void {}
Hrundik
+6  A: 

The reason is that in mxml what you write isn't actually the handler, is what gets executed in the handler. If you compile with -keep-generated-actionscript flag (To set it in Flex Builder right click to open the project Properties, select Flex Compiler, and add -keep-generated-actionscript to the Additional compiler arguments), you can see in the generated source for your component, that the compiler created a handler for that event, and the body is composed by that you wrote in mxml.

So if you have something like:

click="doSomething();"

You can already notice that you're actually giving an instruction there, that's not a method reference you're passing like when you use addEventHandler.

Then you'll have in the generated file something like:

private function myComponent_Click(evt : MouseEvent) : void
{
    doSomething();
}

And somewhere else in the same file the adding of the event listener:

this.addEventListener(MouseEvent.CLICK, myComponent_Click);

Notice the second parameter is not a function result, it's a function reference, because the parenthesizes that denote a function call are missing, and our particular function is not a getter either.

You can also specify more calls in mxml, like:

click="doSomething(); doSomethingElse();"

You can even pass the event parameter to your method:

click="doSomething(event);"

Whatever you write in the value of the mxml event (not sure it's the right term to use) will become the body of the generated handler for the actionscript event.

bug-a-lot