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=’’&gt;
import flash.utils.Dictionary;
public var targetMap: Dictionary = new Dictionary();
public var targetEventMap: Dictionary;
private function addEvent(event:Event, listener:Function, …args ) : void
targetEventMap = targetMap[] == undefined ? new Dictionary : targetMap[];
targetEventMap[event.type] = { listener:listener, args:args };
targetMap[] = targetEventMap; 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()[‘fnOverLoaded’].apply(this, args))
private function fnOverLoaded(…args):void { args.toString(),’arguments ‘+(args.length).toString())
private function fnCreateOverLoaded(…args):void {
var event:Event = args[0] as Event
if(targetMap[] == undefined ){
public function removeEvent( target:IEventDispatcher, type:String ) : void
var targetEventMap : Dictionary = targetMap[target];
delete targetEventMap[type];
target.removeEventListener( type, onEvent );
<Button id=’btn1′ label=’with2Args’ click=”fnCreateOverLoaded(event, handlerFn,’arg1′, ‘arg2′)” />
<Button label=’with4Args’ click=”fnCreateOverLoaded(event, handlerFn,’arg1′, ‘arg2′,’arg3’, ‘arg4’)” />

  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: Logo

You are commenting using your 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: