AS3 function Overloading

The below mxml code demonstrates function overloading in AS3 using …args
fnCreateOverLoaded function can be given with variable number of arguments and handled differently.

<?xml version=’1.0′ encoding=’utf-8′?>
<Application xmlns=’http://www.adobe.com/2006/mxml’&gt;
<Script>
<![CDATA[
import flash.utils.Dictionary;
import flash.events.Event;
public var targetMap: Dictionary = new Dictionary();
public var targetEventMap: Dictionary;
private function addEvent(event:Event, listener:Function, …args ) : void
{
targetEventMap = targetMap[event.target] == undefined ? new Dictionary : targetMap[event.target];
targetEventMap[event.type] = { listener:listener, args:args };
targetMap[event.target] = targetEventMap;
event.target.addEventListener( event.type, onEvent,false,0,true);
}
private function onEvent ( e:Event ) : void
{
targetEventMap = targetMap[e.currentTarget];
if (targetEventMap[e.type].args[0] is Event) targetEventMap[e.type].args.shift();
targetEventMap[e.type].args.unshift( e );
targetEventMap[e.type].listener.apply( e.currentTarget, targetEventMap[e.type].args );
}
private function handlerFn(e:MouseEvent,…args) : void
{
var tempObject:Object = new Object();
var functionName:String = ‘fnc’ + (args.length).toString();
tempObject.functionName = new Function()
tempObject.functionName.call(e.target.parent[‘fnOverLoaded’].apply(this, args))
}
private function fnOverLoaded(…args):void {
mx.controls.Alert.show( args.toString(),’arguments ‘+(args.length).toString())
}
private function fnCreateOverLoaded(…args):void {
var event:Event = args[0] as Event
if(targetMap[event.target] == undefined ){
this[‘addEvent’].apply(this,args)
event.target.dispatchEvent(event)
}
}
public function removeEvent( target:IEventDispatcher, type:String ) : void
{//removeEvent(btn1,MouseEvent.CLICK)
if(targetMap[target]!=undefined){
var targetEventMap : Dictionary = targetMap[target];
delete targetEventMap[type];
target.removeEventListener( type, onEvent );
}
}
]]>
</Script>
<Button id=’btn1′ label=’with2Args’ click=”fnCreateOverLoaded(event, handlerFn,’arg1′, ‘arg2′)” />
<Button label=’with4Args’ click=”fnCreateOverLoaded(event, handlerFn,’arg1′, ‘arg2′,’arg3’, ‘arg4’)” />
</Application>

  1. Very helpfull and clever.
    I was having a hard time understanding the Dictionary class. Your example was very usefull.

    • Jakir
    • November 13th, 2009

    Hi this is good but i got an error in button tag

    Element type “Button” must be followed by either attribute specifications, “>” or “/>”.

    My code is in flex 3.2

    • atul
    • July 6th, 2010

    Please put this code it working fine
    ————————————–

    • atul
    • July 6th, 2010
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: